Преглед на файлове

V0.9.2
1、新增了QTableView的代理类

apple преди 3 седмици
родител
ревизия
b70c2d3a86

+ 2 - 2
CMakeLists.txt

@@ -195,10 +195,10 @@ file(GLOB GLOBAL_SRC
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/VideoPlayer)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/xlsx)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/DesignerPattern)
-# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ViewModel)
+add_subdirectory(${CMAKE_SOURCE_DIR}/demo/ViewModel)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/OpenGLWidgetLibrary)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/VideoPlayerGL)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/OpenGLWidgetTest)
 # add_subdirectory(${CMAKE_SOURCE_DIR}/demo/VideoPlayerLibrary)
-add_subdirectory(${CMAKE_SOURCE_DIR}/demo/RecordAudio)
+# add_subdirectory(${CMAKE_SOURCE_DIR}/demo/RecordAudio)
 

+ 1 - 1
External

@@ -1 +1 @@
-Subproject commit 03328e357ce4a7f94647a209b3553c1cbfc2a313
+Subproject commit 81335eb933226d355c48eac68fa90f2bfce05719

+ 1 - 1
demo/DesignerPattern/CMakeLists.txt

@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.5)
+cmake_minimum_required(VERSION 3.10)
 
 set(this_exe DesignerPattern)
 

+ 2 - 0
demo/ViewModel/CMakeLists.txt

@@ -13,6 +13,7 @@ file(GLOB LOCAL_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
 
     ${CMAKE_CURRENT_SOURCE_DIR}/Base/*.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/Delegate/*.cpp
 )
 
 
@@ -34,6 +35,7 @@ target_include_directories(${this_exe} PRIVATE
     ${CMAKE_SOURCE_DIR}/External/module/Logs
 
     ${CMAKE_CURRENT_SOURCE_DIR}/Base
+    ${CMAKE_CURRENT_SOURCE_DIR}/Delegate
 
     ${spdlog_INCLUDE_DIR}
 )

+ 75 - 0
demo/ViewModel/Delegate/CheckBoxDelegate.cpp

@@ -0,0 +1,75 @@
+#include "CheckBoxDelegate.h"
+
+#include <QCheckBox>
+#include <QPainter>
+#include <QApplication>
+
+
+CheckBoxDelegate::CheckBoxDelegate(QObject *parent) 
+    : QStyledItemDelegate(parent)
+{
+}
+
+
+CheckBoxDelegate::~CheckBoxDelegate()
+{
+
+}
+
+QWidget* CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    // QCheckBox *checkBox = new QCheckBox(parent);
+    // checkBox->setText("Check me");
+    // return checkBox;
+    return nullptr;
+}
+
+void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+
+}
+
+void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+
+}
+
+void CheckBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+
+}
+
+
+void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    // 获取复选框状态
+    bool checked = index.data(Qt::CheckStateRole).toInt() == Qt::Checked;
+    // 获取文本
+    QString text = index.data(Qt::DisplayRole).toString();
+
+    QStyleOptionButton checkBoxOption;
+    QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkBoxOption);
+    checkBoxOption.rect = QRect(option.rect.left(), option.rect.top() + (option.rect.height() - checkBoxRect.height()) / 2, checkBoxRect.width(), checkBoxRect.height());
+    checkBoxOption.state = QStyle::State_Enabled | (checked ? QStyle::State_On : QStyle::State_Off);
+
+    // 绘制复选框
+    QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter);
+
+    // 绘制文本
+    QRect textRect = option.rect;
+    textRect.setLeft(checkBoxOption.rect.right() + 4); // 复选框后留点间距
+    painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text);
+}
+
+bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
+{
+    if (event->type() == QEvent::MouseButtonRelease) 
+    {
+        // Handle checkbox toggle logic here
+        bool currentState = index.data(Qt::CheckStateRole).toBool();
+        model->setData(index, !currentState, Qt::CheckStateRole);
+        return true;
+    }
+    return QStyledItemDelegate::editorEvent(event, model, option, index);
+}
+

+ 31 - 0
demo/ViewModel/Delegate/CheckBoxDelegate.h

@@ -0,0 +1,31 @@
+#ifndef __CHECKBOXDELEGATE_H__
+#define __CHECKBOXDELEGATE_H__
+
+
+#include <QStyledItemDelegate>
+
+
+class CheckBoxDelegate : public QStyledItemDelegate
+{
+    Q_OBJECT
+
+public:
+    explicit CheckBoxDelegate(QObject *parent = nullptr);
+    ~CheckBoxDelegate() override;
+
+    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
+    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
+    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
+    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
+
+    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
+    
+protected:
+    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
+
+};
+
+
+
+
+#endif // __CHECKBOXDELEGATE_H__

+ 6 - 2
demo/ViewModel/main.cpp

@@ -5,14 +5,18 @@
 
 #include "Base/viewmodelbase.h"
 
+#include "widget.h"
+
 
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
     init_log();
     
-    ViewModelBase viewmodel;
-    viewmodel.show();
+    // ViewModelBase viewmodel;
+    // viewmodel.show();
+    Widget w;
+    w.show();
 
     return a.exec();
 }

+ 19 - 1
demo/ViewModel/widget.cpp

@@ -13,8 +13,10 @@ Widget::Widget(QWidget *parent)
     ui->setupUi(this);
 
 
-
     SPDLOG_INFO("✨✨✨✨✨ Qt Library ✨✨✨✨✨");
+
+    initTableView();
+
 }
 
 Widget::~Widget()
@@ -24,6 +26,22 @@ Widget::~Widget()
 }
 
 
+/* 初始化表格 */
+void Widget::initTableView()
+{
+    m_model = new QStandardItemModel(this);
+    m_model->setColumnCount(3);
+    m_model->setRowCount(5);
+    m_model->setHeaderData(0, Qt::Horizontal, "Column 1");
+    m_model->setHeaderData(1, Qt::Horizontal, "Column 2");
+    m_model->setHeaderData(2, Qt::Horizontal, "Column 3");
+
+    ui->tableView->setModel(m_model);
+
+    CheckBoxDelegate *checkBoxDelegate = new CheckBoxDelegate(this);
+    ui->tableView->setItemDelegateForColumn(0, checkBoxDelegate);
+}
+
 
 
 

+ 16 - 0
demo/ViewModel/widget.h

@@ -2,6 +2,13 @@
 #define WIDGET_H
 
 #include <QWidget>
+#include <QStandardItemModel>
+#include <qstandarditemmodel.h>
+
+
+#include "CheckBoxDelegate.h"
+
+
 
 QT_BEGIN_NAMESPACE
 namespace Ui { class Widget; }
@@ -15,11 +22,20 @@ public:
     Widget(QWidget *parent = nullptr);
     ~Widget();
 
+private:
+    /* 初始化表格 */
+    void initTableView();
+
+
 private slots:
 
 
+
+
 private:
     Ui::Widget *ui;
 
+    QStandardItemModel* m_model = nullptr;
+
 };
 #endif // WIDGET_H

+ 11 - 11
demo/ViewModel/widget.ui

@@ -13,17 +13,17 @@
   <property name="windowTitle">
    <string>Widget</string>
   </property>
-  <widget class="QWidget" name="widget_pBtn" native="true">
-   <property name="geometry">
-    <rect>
-     <x>30</x>
-     <y>30</y>
-     <width>681</width>
-     <height>311</height>
-    </rect>
-   </property>
-   <layout class="QGridLayout" name="gridLayout"/>
-  </widget>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QWidget" name="widget_pBtn" native="true">
+     <layout class="QGridLayout" name="gridLayout">
+      <item row="0" column="0">
+       <widget class="QTableView" name="tableView"/>
+      </item>
+     </layout>
+    </widget>
+   </item>
+  </layout>
  </widget>
  <resources/>
  <connections/>