QT表格双击弹出对话框

QT表格双击弹出对话框

QT版本5.0.3

1.效果预览

在这里插入图片描述

2.新建项目

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

在这里插入图片描述

3.添加表格类

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

在这里插入图片描述

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

在这里插入图片描述

将会生成两个文件:mytablewidget.hmytablewidget.cppmytablewidget.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,否则运行不成功。

在这里插入图片描述

运行效果

在这里插入图片描述

  • 15
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安布奇

喜欢的朋友给点支持和鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值