修改完成tableview属性
parent
f2bc7f502b
commit
bce2956dae
|
@ -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);
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ private:
|
|||
|
||||
protected:
|
||||
void Refresh();
|
||||
void InitializeTableView(MyTableModel *model, QTableView *tableView);
|
||||
|
||||
// 重写resizeEvent,当QTableView窗口大小变化时按比例调整列宽
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
#include "devproppage.h"
|
||||
#include "ui_devproppage.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QScrollBar>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QHeaderView>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
#ifndef DEVPROPPAGE_H
|
||||
#define DEVPROPPAGE_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QPushButton>
|
||||
#include <QTableView>
|
||||
|
||||
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<QVector<QString>> data_;
|
||||
};
|
||||
|
||||
|
||||
#endif // DEVPROPPAGE_H
|
|
@ -1,19 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>DevPropPage</class>
|
||||
<widget class="QWidget" name="DevPropPage">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>713</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
#include <QStandardItemModel>
|
||||
|
||||
#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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue