QT表格双击弹出对话框

mac2024-05-10  30

QT表格双击弹出对话框

QT版本5.0.3

文章目录

QT表格双击弹出对话框1.效果预览2.新建项目3.添加表格类4.创建对话框类4.1 创建对话框类4.2 在mydialog.h中定义对话框widget4.3 在mydialog.cpp实例化对话框 5.将表格添加到主窗口以及弹出对话框5.1在mainwindow.h中声明表格和对话框5.2 在mainwindow.cpp中实例化表格和对话框并实现显示对话框 6.运行

1.效果预览

2.新建项目

打开QT,文件>新建文件或项目>选择应用程序,选择QTGUI应用>输入项目名称:Table>一直选择下一步>完成。注意取消勾选创建界面按钮。

3.添加表格类

在项目文件下右键单击>添加新文件>C++类>将类名MyTableWidget,基类设置为QTableWidget>点击下一步直到完成。

记得在输入类名界面将基类的类型信息改为QWidget。

将会生成两个文件:mytablewidget.h和mytablewidget.cpp。mytablewidget.h负责定义表格属性,mytablewidget.cpp负责实现表格。修改mytablewidget.cpp如下:

#include "mytablewidget.h" //引入表头视图 #include <QHeaderView> #include <QPushButton> MyTableWidget::MyTableWidget(QWidget *parent): QTableWidget(parent) { setColumnCount(9);//设置列数 setRowCount(10);//设置行数 //表头 setHorizontalHeaderLabels(QStringList()<<"编号"<<"姓名"<<"年龄" <<"受教育程度"<<"民族" <<"专业"<<"职称"<<"部门"<<"职务"); //表头单元格背景色和字体 setStyleSheet(QString( "QTableWidget QHeaderView::section{background:#2a9ee4;font-weight:bold;}")); horizontalHeader()->setStretchLastSection(true);//设置表格的列随窗口拉伸 verticalHeader()->hide();//隐藏竖直表头 horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //表格列宽自适应 setSelectionBehavior(QAbstractItemView::SelectRows);//单击选择类型为行 setEditTriggers(QAbstractItemView::NoEditTriggers);//禁用表格双击修改 }

mytablewidget.h目前还不需要设置。

4.创建对话框类

4.1 创建对话框类

在项目文件下右键单击>添加新文件>C++类>将类名MyDialog,基类设置为QDialog>点击下一步直到完成。

4.2 在mydialog.h中定义对话框widget

#ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QGridLayout> #include <QDialog> #include <QLineEdit> #include <QComboBox> #include <QHBoxLayout> class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = 0); private://定义对话框控件 QLabel *noLabel;//编号标签 QLineEdit *noInput;//编号输入框 QLabel *nameLabel;//姓名标签 QLineEdit *nameInput;//姓名输入框 QLabel *ageLabel;//年龄标签 QLineEdit *ageInput;//年龄输入框 QLabel *educationLabel;//受教育程度标签 QComboBox *educationComBox;//受教育程度下拉选择框 QLabel *nationLabel;//民族标签 QComboBox *nationComBox;//民族下拉选择框 QLabel *majorLabel;//专业标签 QLineEdit *majorInput;//专业输入框 QLabel *positionLabel;//职称标签 QComboBox *positionComBox;//职称下拉选择框 QLabel *departmentLabel;//部门标签 QComboBox *departmentComBox;//部门下拉选择框 QLabel *jobLabel;//职务标签 QComboBox *jobComBox;//职务下拉选择框 QPushButton *saveButton;//保存修改按钮 QPushButton *cancelButton;//取消修改按钮 QPushButton *closeButton;//关闭对话框按钮 QGridLayout *bottomBarLayout;//保存上述按钮的布局 QGridLayout *mainLayout;//对话框主布局 signals: public slots: }; #endif // MYDIALOG_H

4.3 在mydialog.cpp实例化对话框

#include "mydialog.h" MyDialog::MyDialog(QWidget *parent) : QDialog(parent) { //实例化头文件中声明的控件 noLabel=new QLabel;//编号标签 noLabel->setText(tr("编号:")); noInput = new QLineEdit;//编号输入框 nameLabel=new QLabel;//姓名标签 nameLabel->setText(tr("姓名")); nameInput = new QLineEdit;//姓名输入框 ageLabel = new QLabel(tr("年龄:"));//年龄标签 ageInput = new QLineEdit;//年龄输入框 educationLabel=new QLabel;//受教育程度标签 educationLabel->setText(tr("受教育程度:")); educationComBox = new QComboBox;//受教育程度下拉选择框 QStringList educationList;//受教育程度列表 educationList << "小学"<<"初中"<<"高中"<<"本科"<<"专科"<<"硕士"<<"博士"; for(int i=0;i<educationList.size();i++){//添加下拉框选项 educationComBox->addItem(educationList.at(i)); } nationLabel=new QLabel;//民族标签 nationLabel->setText(tr("民族:")); nationComBox = new QComboBox;//民族下拉选择框 QStringList nationList;//民族列表 nationList<<"壮族"<<"藏族"<<"裕固族"<<"彝族"<<"瑶族"<<"锡伯族"<<"乌孜别克族" <<"维吾尔族"<<"佤族"<<"土家族"<<"土族"<<"塔塔尔族"<<"塔吉克族"<<"水族" <<"畲族"<<"撒拉族"<<"羌族"<<"普米族"<<"怒族"<<"纳西族"<<"仫佬族" <<"苗族"<<"蒙古族"<<"门巴族"<<"毛南族"<<"满族"<<"珞巴族"<<"僳僳族" <<"黎族"<<"拉祜族"<<"柯尔克孜族"<<"景颇族"<<"京族"<<"基诺族"<<"回族" <<"赫哲族"<<"哈萨克族"<<"哈尼族"<<"仡佬族"<<"高山族"<<"鄂温克族"<<"鄂伦春族" <<"独龙族"<<"东乡族"<<"侗族"<<"德昂族"<<"傣族"<<"达斡尔族"<<"朝鲜族" <<"布依族"<<"保安族"<<"布朗族"<<"白族"<<"阿昌族"<<"汉族"<<"俄罗斯族"; for(int i=0;i<nationList.size();i++){ nationComBox->addItem(nationList.at(i));//添加民族选项 } majorLabel=new QLabel;//专业标签 majorLabel->setText(tr("专业:")); majorInput = new QLineEdit;//专业输入框 positionLabel=new QLabel;//职称标签 positionLabel->setText(tr("职称:")); positionComBox = new QComboBox; QStringList positionList; positionList<<"助理工程师"<<"工程师" <<"高级工程师"<<"教授级高级工程师"; for(int i=0;i<positionList.size();i++){ positionComBox->addItem(positionList.at(i)); } departmentLabel=new QLabel;//部门标签 departmentLabel->setText(tr("部门:")); departmentComBox = new QComboBox;//部门下拉选择框 QStringList departmentList; departmentList<<"人事部"<<"技术部" <<"后勤部"<<"信息部"; for(int i=0;i<departmentList.size();i++){ departmentComBox->addItem(departmentList.at(i)); } jobLabel=new QLabel;//职务标签 jobLabel->setText(tr("职务:")); jobComBox = new QComboBox; QStringList jobList; jobList<<"普通员工"<<"工程师" <<"团队领导"<<"部门领导"<<"公司领导"; for(int i=0;i<jobList.size();i++){ jobComBox->addItem(jobList.at(i)); } saveButton=new QPushButton;//保存修改按钮 saveButton->setText(tr("保存")); cancelButton=new QPushButton;//取消修改按钮 cancelButton->setText(tr("取消"));//取消修改按钮 closeButton = new QPushButton(tr("关闭"));//关闭对话框按钮 bottomBarLayout = new QGridLayout;//保存上述按钮的布局 bottomBarLayout->setSpacing(10); bottomBarLayout->setMargin(15); //添加按钮到底部栏 bottomBarLayout->addWidget(saveButton,0,0); bottomBarLayout->addWidget(cancelButton,0,1); bottomBarLayout->addWidget(closeButton,0,2); mainLayout=new QGridLayout(this);//实例化对话框主布局 //添加窗口控件到主布局 //编号 mainLayout->addWidget(noLabel,0,0); mainLayout->addWidget(noInput,0,1); //姓名 mainLayout->addWidget(nameLabel,1,0); mainLayout->addWidget(nameInput,1,1); //年龄 mainLayout->addWidget(ageLabel,2,0); mainLayout->addWidget(ageInput,2,1); //受教育程度 mainLayout->addWidget(educationLabel,3,0); mainLayout->addWidget(educationComBox,3,1); //民族 mainLayout->addWidget(nationLabel,4,0); mainLayout->addWidget(nationComBox,4,1); //专业 mainLayout->addWidget(majorLabel,5,0); mainLayout->addWidget(majorInput,5,1); //职称 mainLayout->addWidget(positionLabel,6,0); mainLayout->addWidget(positionComBox,6,1); //部门 mainLayout->addWidget(departmentLabel,7,0); mainLayout->addWidget(departmentComBox,7,1); //职务 mainLayout->addWidget(jobLabel,8,0); mainLayout->addWidget(jobComBox,8,1); //添加底部工具条布局 mainLayout->addLayout(bottomBarLayout,9,0); }

5.将表格添加到主窗口以及弹出对话框

5.1在mainwindow.h中声明表格和对话框

在mainwindow.h中声明表格和对话框,并声明一个显示对话框的槽函数(slots)

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <mytablewidget.h> #include <mydialog.h> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: //表格 MyTableWidget *table; //对话框 MyDialog *dialog; private slots: //显示对话框 void showDialog(); }; #endif // MAINWINDOW_H

5.2 在mainwindow.cpp中实例化表格和对话框并实现显示对话框

#include "mainwindow.h" #include <mytablewidget.h> #include <mydialog.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setMinimumSize(1200,600);//设置窗口的最小尺寸1200x800 table = new MyTableWidget(this);//实例化表格 setCentralWidget(table);//添加表格到窗口中 dialog = new MyDialog(this); //关联表格点击和显示对话框的槽函数 connect(table,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(showDialog())); } MainWindow::~MainWindow() { } //实现显示对话框的方法 void MainWindow::showDialog(){ dialog->show();//显示弹窗 }

6.运行

注意:运行之前请取消项目>Shadow Build,否则运行不成功。

运行效果

最新回复(0)