tabview signal
海前 王
Posted on September 2, 2024
#include <QApplication>
#include <QMainWindow>
#include <QTableView>
#include <QStandardItemModel>
#include <QDebug>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
class MyTableView : public QTableView {
Q_OBJECT
public:
MyTableView(QWidget *parent = nullptr) : QTableView(parent) {
// 创建模型并设置数据
model = new QStandardItemModel(5, 3, this); // 5 行 3 列
for (int row = 0; row < 5; ++row) {
for (int col = 0; col < 3; ++col) {
QStandardItem *item = new QStandardItem(QString("(%1, %2)").arg(row).arg(col));
model->setItem(row, col, item);
}
}
setModel(model);
// 连接信号和槽
connect(horizontalHeader(), &QHeaderView::sectionResized, this, &MyTableView::onColumnResized);
connect(verticalHeader(), &QHeaderView::sectionResized, this, &MyTableView::onRowResized);
connect(horizontalHeader(), &QHeaderView::sectionMoved, this, &MyTableView::onColumnMoved);
connect(verticalHeader(), &QHeaderView::sectionMoved, this, &MyTableView::onRowMoved);
}
public Q_SLOTS:
void resizeRow(int row, int height) {
verticalHeader()->resizeSection(row, height);
qDebug() << "Row" << row << "resized to height" << height;
}
void resizeColumn(int column, int width) {
horizontalHeader()->resizeSection(column, width);
qDebug() << "Column" << column << "resized to width" << width;
}
void removeRow(int row) {
model->removeRow(row);
qDebug() << "Row" << row << "removed.";
}
void removeColumn(int column) {
model->removeColumn(column);
qDebug() << "Column" << column << "removed.";
}
protected Q_SLOTS:
void onRowResized(int row, int oldHeight, int newHeight) {
qDebug() << "Row resized:" << row << "from height" << oldHeight << "to" << newHeight;
}
void onColumnResized(int column, int oldWidth, int newWidth) {
qDebug() << "Column resized:" << column << "from width" << oldWidth << "to" << newWidth;
}
void onRowMoved(int row, int oldIndex, int newIndex) {
qDebug() << "Row moved from index" << oldIndex << "to" << newIndex;
}
void onColumnMoved(int column, int oldIndex, int newIndex) {
qDebug() << "Column moved from index" << oldIndex << "to" << newIndex;
}
private:
QStandardItemModel *model;
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
tableView = new MyTableView(this);
setCentralWidget(tableView);
// 创建工具栏和动作
QToolBar *toolBar = addToolBar("Main Toolbar");
QAction *resizeRowAction = new QAction("Resize Row 2", this);
QAction *resizeColumnAction = new QAction("Resize Column 1", this);
QAction *removeRowAction = new QAction("Remove Row 3", this);
QAction *removeColumnAction = new QAction("Remove Column 2", this);
connect(resizeRowAction, &QAction::triggered, this, &MainWindow::resizeRow);
connect(resizeColumnAction, &QAction::triggered, this, &MainWindow::resizeColumn);
connect(removeRowAction, &QAction::triggered, this, &MainWindow::removeRow);
connect(removeColumnAction, &QAction::triggered, this, &MainWindow::removeColumn);
toolBar->addAction(resizeRowAction);
toolBar->addAction(resizeColumnAction);
toolBar->addAction(removeRowAction);
toolBar->addAction(removeColumnAction);
// 创建菜单栏
QMenu *editMenu = menuBar()->addMenu("Edit");
editMenu->addAction(resizeRowAction);
editMenu->addAction(resizeColumnAction);
editMenu->addAction(removeRowAction);
editMenu->addAction(removeColumnAction);
}
private Q_SLOTS:
void resizeRow() {
tableView->resizeRow(2, 60); // Resize row 2 to 60
}
void resizeColumn() {
tableView->resizeColumn(1, 100); // Resize column 1 to 100
}
void removeRow() {
tableView->removeRow(3); // Remove row 3
}
void removeColumn() {
tableView->removeColumn(2); // Remove column 2
}
private:
MyTableView *tableView;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.resize(800, 600);
window.show();
return app.exec();
}
#include "main.moc"
💖 💪 🙅 🚩
海前 王
Posted on September 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024