aaaa
parent
d4191b4daf
commit
f2bc7f502b
|
@ -7,6 +7,7 @@
|
|||
#include <QVBoxLayout>
|
||||
#include <QPushButton>
|
||||
#include <QScrollBar>
|
||||
#include <QHeaderView>
|
||||
|
||||
#include "globalparameters.h"
|
||||
#include "mytablemodel.h"
|
||||
|
@ -14,12 +15,11 @@
|
|||
DevicePropertyPage::DevicePropertyPage(QWidget *parent) :
|
||||
QWidget(parent),m_pTableView(nullptr),m_pButton(nullptr)
|
||||
{
|
||||
//InitializeTable();
|
||||
InitializeTable();
|
||||
|
||||
m_pTimer = new QTimer(this);
|
||||
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
|
||||
//m_pTimer->start(AppData::getInstance()->nTimeOut);
|
||||
|
||||
}
|
||||
|
||||
DevicePropertyPage::~DevicePropertyPage()
|
||||
|
@ -34,7 +34,7 @@ void DevicePropertyPage::handleTimeout()
|
|||
|
||||
Refresh();
|
||||
|
||||
int nInterval = 5000; //ui->spinBox->value();
|
||||
int nInterval = 5000;
|
||||
m_pTimer->start(nInterval*1000);
|
||||
}
|
||||
}
|
||||
|
@ -50,11 +50,11 @@ void DevicePropertyPage::InitializeTable()
|
|||
// 创建主布局
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||
|
||||
//AppCommon::getInstance()->InitializeTableView(m_myModel,m_pTableView);
|
||||
m_pTableView->setModel(m_myModel);
|
||||
// 自动调整列宽
|
||||
connect(m_pTableView->horizontalScrollBar(), &QScrollBar::valueChanged, this, &DevicePropertyPage::adjustColumnWidths);
|
||||
adjustColumnWidths(); // 初次设置列宽
|
||||
//m_pTableView->setModel(m_myModel);
|
||||
|
||||
AppCommon::getInstance()->InitializeTableView(m_myModel,m_pTableView);
|
||||
|
||||
|
||||
|
||||
mainLayout->addWidget(m_pTableView);
|
||||
|
||||
|
@ -76,6 +76,8 @@ void DevicePropertyPage::InitializeTable()
|
|||
// 连接信号和槽
|
||||
connect(m_pTableView, &QTableView::doubleClicked, this, &DevicePropertyPage::onTableViewDoubleClicked);
|
||||
connect(m_pButton, &QPushButton::clicked, this, &DevicePropertyPage::onButtonClicked);
|
||||
|
||||
setLayout(mainLayout);
|
||||
}
|
||||
|
||||
void DevicePropertyPage::Refresh()
|
||||
|
@ -112,14 +114,27 @@ void DevicePropertyPage::onTableViewDoubleClicked(const QModelIndex &index)
|
|||
QVariant datatemp=model->data(mindex);
|
||||
}
|
||||
|
||||
void DevicePropertyPage::adjustColumnWidths()
|
||||
void DevicePropertyPage::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
int columnCount = m_pTableView->model()->columnCount();
|
||||
int totalWidth = m_pTableView->viewport()->width();
|
||||
int columnWidth = totalWidth / columnCount;
|
||||
for (int column = 0; column < columnCount; ++column)
|
||||
{
|
||||
m_pTableView->setColumnWidth(column, columnWidth);
|
||||
QWidget::resizeEvent(event);
|
||||
#if 0
|
||||
// 获取当前QTableView的总宽度
|
||||
int currentTotalWidth = m_pTableView->viewport()->width();
|
||||
|
||||
// 计算总的初始宽度
|
||||
int initialTotalWidth = 0;
|
||||
for (int width : columnWidths) {
|
||||
initialTotalWidth += width;
|
||||
}
|
||||
|
||||
// 计算比例因子
|
||||
double scaleFactor = static_cast<double>(currentTotalWidth) / initialTotalWidth;
|
||||
|
||||
// 重新设置列宽,按比例调整
|
||||
for (int i = 0; i < columnWidths.size(); ++i) {
|
||||
int newWidth = static_cast<int>(columnWidths[i] * scaleFactor);
|
||||
m_pTableView->setColumnWidth(i, newWidth);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -31,21 +31,23 @@ private:
|
|||
MyTableModel* m_myModel;
|
||||
|
||||
protected:
|
||||
|
||||
void Refresh();
|
||||
|
||||
// 重写resizeEvent,当QTableView窗口大小变化时按比例调整列宽
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void onButtonClicked();
|
||||
|
||||
void onTableViewDoubleClicked(const QModelIndex &index);
|
||||
|
||||
void adjustColumnWidths();
|
||||
|
||||
private:
|
||||
unsigned int filterBaseType;
|
||||
unsigned int mask_code;
|
||||
QTableView* m_pTableView;
|
||||
QPushButton* m_pButton;
|
||||
QVector<int> columnWidths; // 存储初始列宽
|
||||
};
|
||||
|
||||
#endif // DEVICEPROPERTYPAGE_H
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
#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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#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
|
|
@ -0,0 +1,19 @@
|
|||
<?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,6 +41,7 @@ win32:LIBS += Ws2_32.lib
|
|||
SOURCES += \
|
||||
datafetcher.cpp \
|
||||
devicepropertypage.cpp \
|
||||
devproppage.cpp \
|
||||
globalparameters.cpp \
|
||||
main.cpp \
|
||||
maindialog.cpp \
|
||||
|
@ -50,6 +51,7 @@ SOURCES += \
|
|||
HEADERS += \
|
||||
datafetcher.h \
|
||||
devicepropertypage.h \
|
||||
devproppage.h \
|
||||
globalparameters.h \
|
||||
maindialog.h \
|
||||
mainwindow.h \
|
||||
|
@ -57,6 +59,7 @@ HEADERS += \
|
|||
singleton.h
|
||||
|
||||
FORMS += \
|
||||
devproppage.ui \
|
||||
maindialog.ui \
|
||||
mainwindow.ui
|
||||
|
||||
|
|
|
@ -60,6 +60,9 @@ 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);
|
||||
|
@ -73,6 +76,7 @@ void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView)
|
|||
//设置表格数据区内的所有单元格都不允许编辑
|
||||
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
|
||||
#if 0
|
||||
//设置列宽
|
||||
int w = tableView->width();
|
||||
int base = w /(AppData::getInstance()->lstDataTableHeaderText.count()-1);
|
||||
|
@ -81,6 +85,11 @@ void AppCommon::InitializeTableView(MyTableModel *model, QTableView *tableView)
|
|||
tableView->setColumnWidth(2,base+30);
|
||||
tableView->setColumnWidth(3,base+10);
|
||||
tableView->setColumnWidth(4,base);
|
||||
#endif
|
||||
|
||||
// 设置QTableView的列宽度根据总宽度平均分配
|
||||
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
//tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
|
||||
|
||||
tableView->show();
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <QStandardItemModel>
|
||||
|
||||
#include "devicepropertypage.h"
|
||||
#include "devproppage.h"
|
||||
|
||||
MainDialog::MainDialog(QWidget *parent) :
|
||||
QMainWindow (parent),
|
||||
|
@ -26,8 +27,6 @@ MainDialog::MainDialog(QWidget *parent) :
|
|||
|
||||
//InitializeUI();
|
||||
|
||||
//
|
||||
|
||||
// Load the window state
|
||||
//loadWindowState();
|
||||
}
|
||||
|
@ -130,11 +129,12 @@ void MainDialog::CreateTablePage()
|
|||
{
|
||||
m_pDevicestackedWidget = new QStackedWidget(this);
|
||||
|
||||
QVBoxLayout *stackedWidgetLayout = new QVBoxLayout();
|
||||
m_pDevicestackedWidget->setLayout(stackedWidgetLayout);
|
||||
QVBoxLayout *stackedWidgetLayout = new QVBoxLayout(this);
|
||||
//m_pDevicestackedWidget->setLayout(stackedWidgetLayout);
|
||||
stackedWidgetLayout->addWidget(m_pDevicestackedWidget);
|
||||
|
||||
DevicePropertyPage *page1 = new DevicePropertyPage(m_pDevicestackedWidget);
|
||||
DevicePropertyPage *page2 = new DevicePropertyPage(m_pDevicestackedWidget);
|
||||
DevPropPage *page2 = new DevPropPage(m_pDevicestackedWidget);
|
||||
|
||||
m_pDevicestackedWidget->addWidget(page1); // Add QTableView as a page
|
||||
m_pDevicestackedWidget->addWidget(page2);
|
||||
|
@ -144,8 +144,7 @@ void MainDialog::CreateTablePage()
|
|||
m_pDevicestackedWidget->addWidget(new QPushButton("Page 5"));
|
||||
m_pDevicestackedWidget->addWidget(new QPushButton("Page 6"));
|
||||
|
||||
page1->InitializeTable();
|
||||
page2->InitializeTable();
|
||||
setLayout(stackedWidgetLayout);
|
||||
|
||||
}
|
||||
void MainDialog::CreateIcon(const QIcon& icon,QString text)
|
||||
|
|
|
@ -47,8 +47,10 @@ 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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue