From bce2956dae20109467a3385b24f36983f0e31914 Mon Sep 17 00:00:00 2001 From: hkc320 Date: Sat, 14 Sep 2024 13:01:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=8C=E6=88=90tableview?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../emsConfigurer/devicepropertypage.cpp | 72 +++++++++++++-- .../emsConfigurer/devicepropertypage.h | 1 + applications/emsConfigurer/devproppage.cpp | 88 ------------------- applications/emsConfigurer/devproppage.h | 70 --------------- applications/emsConfigurer/devproppage.ui | 19 ---- applications/emsConfigurer/emsConfigurer.pro | 3 - .../emsConfigurer/globalparameters.cpp | 45 ++++++---- applications/emsConfigurer/maindialog.cpp | 33 +++++-- applications/emsConfigurer/mainwindow.cpp | 4 - applications/emsConfigurer/mytablemodel.cpp | 17 +++- 10 files changed, 133 insertions(+), 219 deletions(-) delete mode 100644 applications/emsConfigurer/devproppage.cpp delete mode 100644 applications/emsConfigurer/devproppage.h delete mode 100644 applications/emsConfigurer/devproppage.ui diff --git a/applications/emsConfigurer/devicepropertypage.cpp b/applications/emsConfigurer/devicepropertypage.cpp index fb3e1d4..468c761 100644 --- a/applications/emsConfigurer/devicepropertypage.cpp +++ b/applications/emsConfigurer/devicepropertypage.cpp @@ -39,6 +39,72 @@ void DevicePropertyPage::handleTimeout() } } +void DevicePropertyPage::InitializeTableView(MyTableModel *model, QTableView *tableView) +{ + //设置tableview的model + model->setHeadData(AppData::getInstance()->lstDataTableHeaderText); + tableView->setModel(model); + + QHeaderView* pHeaderView = tableView->horizontalHeader(); + //pHeaderView->setStyleSheet("QHeaderView::section {color: black;padding-left: 4px;border: 1px solid #6c6c6c;}"); + pHeaderView->setStyleSheet("QHeaderView::section{background:lightgray;}"); + + // pHeaderView->setSectionResizeMode(QHeaderView::Stretch); //Stretch + + pHeaderView->setHidden(false); //false 显示行号列 true Hide + + //pHeaderView->setVisible(true); + //pHeaderView->setFixedHeight(40); + + //点击表时不对表头行光亮(获取焦点) + pHeaderView->setHighlightSections(false); + // pHeaderView->setDefaultSectionSize(200); + + // //设置表头字体加粗 + // QFont font = pHeaderView->font(); + // font.setBold(true); + // pHeaderView->setFont(font); + + //设置表头字体 + pHeaderView->setFont(QFont("Arial", 12)); + + // 设置表头列宽自动调整 + for (int i = 0; i < model->columnCount(); ++i) + { + pHeaderView->setSectionResizeMode(i, QHeaderView::Interactive); + } + + // 允许用户通过拖动表头边缘调整列宽 + //pHeaderView->setSectionResizeMode(QHeaderView::Interactive); + + tableView->verticalHeader()->setDefaultSectionSize(30); //行高 + + tableView->setSelectionBehavior(QAbstractItemView::SelectRows); + tableView->setSelectionMode(QAbstractItemView::SingleSelection); + + tableView->setAlternatingRowColors(true); + tableView->setTextElideMode(Qt::ElideMiddle); + + + //所有单元格的字体 设置成一样 + tableView->setFont(QFont("Arial", 9)); + + //设置表格数据区内的所有单元格都不允许编辑 + tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); + +#if 1 + //设置列宽 + int base = 120; //w / (model->columnCount()-1); + tableView->setColumnWidth(0,base-30); + tableView->setColumnWidth(1,base*3+40); + tableView->setColumnWidth(2,base); + tableView->setColumnWidth(3,base-40); + tableView->setColumnWidth(4,base+50); +#endif + + tableView->show(); +} + void DevicePropertyPage::InitializeTable() { m_myModel = new MyTableModel(this); @@ -50,11 +116,7 @@ void DevicePropertyPage::InitializeTable() // 创建主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this); - //m_pTableView->setModel(m_myModel); - - AppCommon::getInstance()->InitializeTableView(m_myModel,m_pTableView); - - + InitializeTableView(m_myModel,m_pTableView); mainLayout->addWidget(m_pTableView); diff --git a/applications/emsConfigurer/devicepropertypage.h b/applications/emsConfigurer/devicepropertypage.h index 1136c5c..f9393c9 100644 --- a/applications/emsConfigurer/devicepropertypage.h +++ b/applications/emsConfigurer/devicepropertypage.h @@ -32,6 +32,7 @@ private: protected: void Refresh(); + void InitializeTableView(MyTableModel *model, QTableView *tableView); // 重写resizeEvent,当QTableView窗口大小变化时按比例调整列宽 void resizeEvent(QResizeEvent *event) override; diff --git a/applications/emsConfigurer/devproppage.cpp b/applications/emsConfigurer/devproppage.cpp deleted file mode 100644 index 12ed315..0000000 --- a/applications/emsConfigurer/devproppage.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "devproppage.h" -#include "ui_devproppage.h" - -#include -#include -#include -#include -#include - -DevPropPage::DevPropPage(QWidget *parent) : - QWidget(parent), - ui(new Ui::DevPropPage) -{ - ui->setupUi(this); - - InitializeUI(); -} - -DevPropPage::~DevPropPage() -{ - delete ui; -} - -void DevPropPage::InitializeUI() -{ - tableView =new QTableView(this); - button = new QPushButton("Click Me", this); - - // 主布局 - QVBoxLayout *mainLayout = new QVBoxLayout(this); - - // 表格视图 - SimpleTableModel* model = new SimpleTableModel(this); - //model->setHorizontalHeaderLabels({"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6"}); - - tableView->setModel(model); - - // 设置QTableView的列宽度根据总宽度平均分配 - QHeaderView *header = tableView->horizontalHeader(); - header->setSectionResizeMode(QHeaderView::Stretch); - - // 自动调整列宽 - //connect(tableView->horizontalScrollBar(), &QScrollBar::valueChanged, this, &DevPropPage::adjustColumnWidths); - - // 添加到主布局 - mainLayout->addWidget(tableView); - - // 按钮容器布局 - QWidget *buttonContainer = new QWidget(this); - QHBoxLayout *buttonLayout = new QHBoxLayout(buttonContainer); - buttonLayout->addStretch(); // 使按钮保持在右侧 - buttonLayout->addWidget(button); - buttonLayout->setContentsMargins(0, 0, 0, 0); // 取消按钮的边距 - mainLayout->addWidget(buttonContainer); - - // 设置按钮的固定大小 - button->setFixedSize(100, 30); - - // 连接信号和槽 - connect(tableView, &QTableView::doubleClicked, this, &DevPropPage::onTableViewDoubleClicked); - connect(button, &QPushButton::clicked, this, &DevPropPage::onButtonClicked); - - // 设置主布局的边距和间距 - mainLayout->setContentsMargins(0, 0, 0, 0); - mainLayout->setSpacing(0); - - setLayout(mainLayout); -} - -void DevPropPage::onTableViewDoubleClicked(const QModelIndex &index) { - // 处理双击事件 - qDebug() << "Cell double-clicked at row:" << index.row() << "column:" << index.column(); -} - -void DevPropPage::onButtonClicked() { - // 处理按钮点击事件 - qDebug() << "Button clicked!"; -} - -void DevPropPage::adjustColumnWidths() { - int columnCount = tableView->model()->columnCount(); - int totalWidth = tableView->viewport()->width(); - int columnWidth = totalWidth / columnCount; - - for (int column = 0; column < columnCount; ++column) { - tableView->setColumnWidth(column, columnWidth); - } -} diff --git a/applications/emsConfigurer/devproppage.h b/applications/emsConfigurer/devproppage.h deleted file mode 100644 index 9e0c048..0000000 --- a/applications/emsConfigurer/devproppage.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef DEVPROPPAGE_H -#define DEVPROPPAGE_H - -#include -#include -#include - -namespace Ui { -class DevPropPage; -} - -class DevPropPage : public QWidget -{ - Q_OBJECT - -public: - explicit DevPropPage(QWidget *parent = nullptr); - ~DevPropPage(); - - - void InitializeUI(); -private slots: - void onTableViewDoubleClicked(const QModelIndex &index); - - void onButtonClicked(); - - void adjustColumnWidths(); - -private: - Ui::DevPropPage *ui; - QTableView* tableView; - QPushButton* button; -}; - -class SimpleTableModel : public QAbstractTableModel { - Q_OBJECT - -public: - SimpleTableModel(QObject *parent = nullptr) - : QAbstractTableModel(parent) { - // 初始化数据(示例数据) - data_.resize(10); // 10行示例数据 - for (int row = 0; row < 10; ++row) { - data_[row].resize(6, QString("Row %1, Col %2").arg(row).arg(row % 6)); - } - } - - int rowCount(const QModelIndex &parent = QModelIndex()) const override { - Q_UNUSED(parent); - return data_.size(); - } - - int columnCount(const QModelIndex &parent = QModelIndex()) const override { - Q_UNUSED(parent); - return 6; // 固定6列 - } - - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { - if (!index.isValid() || role != Qt::DisplayRole) - return QVariant(); - - return data_[index.row()][index.column()]; - } - -private: - QVector> data_; -}; - - -#endif // DEVPROPPAGE_H diff --git a/applications/emsConfigurer/devproppage.ui b/applications/emsConfigurer/devproppage.ui deleted file mode 100644 index 022d6f5..0000000 --- a/applications/emsConfigurer/devproppage.ui +++ /dev/null @@ -1,19 +0,0 @@ - - - DevPropPage - - - - 0 - 0 - 713 - 600 - - - - Form - - - - - diff --git a/applications/emsConfigurer/emsConfigurer.pro b/applications/emsConfigurer/emsConfigurer.pro index 23c62f8..103d31c 100644 --- a/applications/emsConfigurer/emsConfigurer.pro +++ b/applications/emsConfigurer/emsConfigurer.pro @@ -41,7 +41,6 @@ win32:LIBS += Ws2_32.lib SOURCES += \ datafetcher.cpp \ devicepropertypage.cpp \ - devproppage.cpp \ globalparameters.cpp \ main.cpp \ maindialog.cpp \ @@ -51,7 +50,6 @@ SOURCES += \ HEADERS += \ datafetcher.h \ devicepropertypage.h \ - devproppage.h \ globalparameters.h \ maindialog.h \ mainwindow.h \ @@ -59,7 +57,6 @@ HEADERS += \ singleton.h FORMS += \ - devproppage.ui \ maindialog.ui \ mainwindow.ui diff --git a/applications/emsConfigurer/globalparameters.cpp b/applications/emsConfigurer/globalparameters.cpp index 060391e..b7ad80c 100644 --- a/applications/emsConfigurer/globalparameters.cpp +++ b/applications/emsConfigurer/globalparameters.cpp @@ -44,14 +44,36 @@ void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView) model->setHeadData(AppData::getInstance()->lstDataTableHeaderText); tableView->setModel(model); - //tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {color: black;padding-left: 4px;border: 1px solid #6c6c6c;}"); - tableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background:lightgray;}"); - //tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); //Stretch + QHeaderView* pHeaderView = tableView->horizontalHeader(); + //pHeaderView->setStyleSheet("QHeaderView::section {color: black;padding-left: 4px;border: 1px solid #6c6c6c;}"); + pHeaderView->setStyleSheet("QHeaderView::section{background:lightgray;}"); + + // pHeaderView->setSectionResizeMode(QHeaderView::Stretch); //Stretch + + pHeaderView->setHidden(false); //false 显示行号列 true Hide + //pHeaderView->setVisible(true); + //pHeaderView->setFixedHeight(40); //点击表时不对表头行光亮(获取焦点) - tableView->horizontalHeader()->setHighlightSections(false); + pHeaderView->setHighlightSections(false); + //pHeaderView->setDefaultSectionSize(35); + + // //设置表头字体加粗 + // QFont font = pHeaderView->font(); + // font.setBold(true); + // pHeaderView->setFont(font); + + //设置表头字体 + pHeaderView->setFont(QFont("Arial", 12)); + + // 设置表头列宽自动调整 + for (int i = 0; i < model->columnCount(); ++i) { + pHeaderView->setSectionResizeMode(i, QHeaderView::Stretch); + } + + // 允许用户通过拖动表头边缘调整列宽 + pHeaderView->setSectionResizeMode(QHeaderView::Interactive); - tableView->horizontalHeader()->setDefaultSectionSize(35); tableView->verticalHeader()->setDefaultSectionSize(30); //行高 tableView->setSelectionBehavior(QAbstractItemView::SelectRows); @@ -60,16 +82,7 @@ void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView) tableView->setAlternatingRowColors(true); tableView->setTextElideMode(Qt::ElideMiddle); - //tableView->horizontalHeader()->setVisible(true); - //tableView->horizontalHeader()->setFixedHeight(40); -// //设置表头字体加粗 -// QFont font = tableView->horizontalHeader()->font(); -// font.setBold(true); -// tableView->horizontalHeader()->setFont(font); - - //设置表头字体 - tableView->horizontalHeader()->setFont(QFont("Arial", 12)); //所有单元格的字体 设置成一样 tableView->setFont(QFont("Arial", 9)); @@ -87,10 +100,6 @@ void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView) tableView->setColumnWidth(4,base); #endif - // 设置QTableView的列宽度根据总宽度平均分配 - tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); - //tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); - tableView->show(); } diff --git a/applications/emsConfigurer/maindialog.cpp b/applications/emsConfigurer/maindialog.cpp index e7fe05b..d67b18e 100644 --- a/applications/emsConfigurer/maindialog.cpp +++ b/applications/emsConfigurer/maindialog.cpp @@ -15,7 +15,6 @@ #include #include "devicepropertypage.h" -#include "devproppage.h" MainDialog::MainDialog(QWidget *parent) : QMainWindow (parent), @@ -25,10 +24,10 @@ MainDialog::MainDialog(QWidget *parent) : this->setWindowIcon(QIcon(":/images/icon.png")); - //InitializeUI(); + InitializeUI(); // Load the window state - //loadWindowState(); + loadWindowState(); } MainDialog::~MainDialog() @@ -39,6 +38,7 @@ MainDialog::~MainDialog() void MainDialog::InitializeUI() { // Create central widget and layout + this->takeCentralWidget(); QWidget *centralWidget = new QWidget(this); QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget); @@ -83,6 +83,9 @@ void MainDialog::InitializeUI() // Disable splitter dragging splitter->setChildrenCollapsible(false); + // 将工具栏添加到主窗口的顶部 + //addToolBar(Qt::TopToolBarArea, m_pMainToolBar); + // Set up layout mainLayout->addWidget(m_pMainToolBar); mainLayout->addWidget(splitter); @@ -107,6 +110,14 @@ void MainDialog::CreateToolbar() m_pMainToolBar = new QToolBar(this); m_pMainToolBar->setIconSize(QSize(48, 48)); + // 创建一个水平布局以容纳图标和占位符 + QWidget *toolBarWidget = new QWidget(this); + QHBoxLayout *layout = new QHBoxLayout(toolBarWidget); + layout->setContentsMargins(0, 0, 0, 0); // 去除内边距 + + // 添加一个弹性空间以推送图标到右侧 + QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + layout->addItem(spacer); // Create actions for the toolbar QAction *action1 = new QAction(QIcon(":/images/icon.png"), "Button 1", this); @@ -118,23 +129,27 @@ void MainDialog::CreateToolbar() m_pMainToolBar->addAction(action2); m_pMainToolBar->addAction(action3); + // 将工具栏的 widget 设置为包含图标和占位符的 widget + m_pMainToolBar->addWidget(toolBarWidget); + // Connect actions to slots connect(action1, &QAction::triggered, this, &MainDialog::onToolButton1Clicked); connect(action2, &QAction::triggered, this, &MainDialog::onToolButton1Clicked); connect(action3, &QAction::triggered, this, &MainDialog::onAboutButtonClicked); - } void MainDialog::CreateTablePage() { m_pDevicestackedWidget = new QStackedWidget(this); - QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(this); + QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(m_pDevicestackedWidget); + +#if 1 //m_pDevicestackedWidget->setLayout(stackedWidgetLayout); stackedWidgetLayout->addWidget(m_pDevicestackedWidget); DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget); - DevPropPage *page2 = new DevPropPage(m_pDevicestackedWidget); + DevicePropertyPage *page2 = new DevicePropertyPage(m_pDevicestackedWidget); m_pDevicestackedWidget->addWidget(page1); // Add QTableView as a page m_pDevicestackedWidget->addWidget(page2); @@ -143,10 +158,10 @@ void MainDialog::CreateTablePage() m_pDevicestackedWidget->addWidget(new QPushButton("Page 4")); m_pDevicestackedWidget->addWidget(new QPushButton("Page 5")); m_pDevicestackedWidget->addWidget(new QPushButton("Page 6")); - - setLayout(stackedWidgetLayout); - +#endif + // setLayout(stackedWidgetLayout); } + void MainDialog::CreateIcon(const QIcon& icon,QString text) { QListWidgetItem *itemButton = new QListWidgetItem(m_pDeviceListWidget); diff --git a/applications/emsConfigurer/mainwindow.cpp b/applications/emsConfigurer/mainwindow.cpp index 707b484..cb9e0e1 100644 --- a/applications/emsConfigurer/mainwindow.cpp +++ b/applications/emsConfigurer/mainwindow.cpp @@ -47,10 +47,6 @@ void MainWindow::setIp(const QString &ip) void MainWindow::on_pb_Logon_clicked() { m_pMainDialog = new MainDialog(); - - m_pMainDialog->InitializeUI(); - m_pMainDialog->loadWindowState(); - m_pMainDialog->show(); this->close(); } diff --git a/applications/emsConfigurer/mytablemodel.cpp b/applications/emsConfigurer/mytablemodel.cpp index fa68aba..1e7d268 100644 --- a/applications/emsConfigurer/mytablemodel.cpp +++ b/applications/emsConfigurer/mytablemodel.cpp @@ -5,6 +5,16 @@ MyTableModel::MyTableModel(QObject* parent) :QAbstractTableModel(parent) { + for(int i=0;i<100;i++) + { + ModelItem item; + item.status = STATUS_WARN; + item.ParameterName= "Visual Leak Detector detected 1 memory leak (84 bytes)"; + item.sampleTime = "2024-9-10 11:22:33.444"; + item.unit = "C"; + item.value = "100"; + m_modelData.append(item); + } } int MyTableModel::rowCount(const QModelIndex & /*parent*/) const @@ -50,13 +60,14 @@ QVariant MyTableModel::data(const QModelIndex& index, int role) const //const int row = index.row(); switch (index.column()) { + case 1: case 2: case 3: return QVariant(Qt::AlignLeft|Qt::AlignVCenter); break; - case 4: - return QVariant(Qt::AlignRight|Qt::AlignVCenter); - break; + //case 4: + // return QVariant(Qt::AlignRight|Qt::AlignVCenter); + // break; default: return Qt::AlignCenter; break;