zoukankan      html  css  js  c++  java
  • QT-创建对话框

    #ifndef FINDDIALOG_H
    #define FINDDIALOG_H
    
    #include<QDialog>
    
    //前置声明会告诉C++编译程序类的存在,而不用提供类定义中的所有细节。
    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;
    
    //FindDialog 继承 QDialog(对话框)类
    class FindDialog : public QDialog{
            Q_OBJECT//对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必需的。
    public:
        //构造函数  参数为:QWidget部件类
        FindDialog(QWidget *parent = 0);
    
        //signals部分声明了当用户点击Find按钮时对话框所发射的两个信号
    signals:
        //Qt::CaseSensitivity为枚举型,可取值Qt::CaseSensitive和Qt::CaseInsensitive
        //表示匹配的灵敏度
        void findNext(const QString &str,Qt::CaseSensitivity cs);
        void findPrevious(const QString &str,Qt::CaseSensitivity cs);
    
    private slots:
        void findClicked();
        void enableFindButton(const QString &text);
    
    private:
        //因为都是指针,所以可以使用前置类型。
        QLabel *label;
        QLineEdit *lineEdit;
        QCheckBox *backwardCheckBox;
        QCheckBox *caseCheckBox;
        QPushButton *findButton;
        QPushButton *closeButton;
    
    };
    #endif // FINDDIALOG_H

    上面是头文件

    finddialog.h
    下面是
    finddialog.cpp
    #include<QPushButton>
    #include<QLabel>
    #include<QHBoxLayout>
    #include<QVBoxLayout>
    #include<QCheckBox>
    #include<QLineEdit>
    #include"finddialog.h"
    
    FindDialog::FindDialog(QWidget *parent)
        :QDialog(parent)
    {
        //创建了一个带有快捷键ALT+W的标签
        label = new QLabel(tr("Find &what:"));
    
        lineEdit = new QLineEdit;
    
        //设置编辑器作为标签的伙伴。按下标签的快捷键时,lineEdit会接受焦点。
        label->setBuddy(lineEdit);
    
        caseCheckBox = new QCheckBox(tr("Match &case"));
    
        backwardCheckBox = new QCheckBox(tr("Search &backward"));
    
        //Alt+F快捷键会激活Find按钮
        findButton = new QPushButton(tr("&Find"));
    
        //将Find按钮作为对话框的默认按钮。默认按钮就是当用户按下Enter键时能够按下对应的按钮
        findButton->setDefault(true);
        //将Find按钮禁止
        findButton->setEnabled(false);
    
        closeButton = new QPushButton(tr("Close"));
    
        //当lineEdit里面的内容发生变化时,会调用本类的enableFindButton函数。
        QObject::connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString &)));
    
        //当点击Find按钮时,会调用本类的findClicked方法.
        QObject::connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    
        //当点击Close按钮时,会调用本类或者父类的close方法
        QObject::connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    
        //水平布局  加入部件label  lineEdit
        QHBoxLayout *topLeftLayout = new QHBoxLayout;
        topLeftLayout->addWidget(label);
        topLeftLayout->addWidget(lineEdit);
    
        //垂直布局  加入水平布局里所有的控件,接着加入caseCheckBox
        QVBoxLayout *leftLayout = new QVBoxLayout;
        leftLayout->addLayout(topLeftLayout);
        leftLayout->addWidget(caseCheckBox);
    
        //垂直布局 加入两个按钮部件
        QVBoxLayout *rightLayout = new QVBoxLayout;
        rightLayout->addWidget(findButton);
        rightLayout->addWidget(closeButton);
        //实现布局器的空白控件分配
        rightLayout->addStretch();
    
        //水平布局 加入之前布局的所有控件
        QHBoxLayout *mainLayout = new QHBoxLayout;
        mainLayout->addLayout(leftLayout);
        mainLayout->addLayout(rightLayout);
        //实现该控件的布局
        setLayout(mainLayout);
    
        setWindowTitle("Find");
        setFixedHeight(sizeHint().height());
    
    }
    void FindDialog::findClicked(){
    
            QString text = lineEdit->text();
            Qt::CaseSensitivity cs =
                      caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
    
            if(backwardCheckBox->isChecked()){
                emit findPrevious(text,cs);
    
            }else{
                emit findNext(text,cs);
            }
    
    }
    void FindDialog::enableFindButton(const QString &text){
        findButton->setEnabled(!text.isEmpty());
    }

    main文件

    1 #include "mainwindow.h"
    2 #include <QApplication>
    3 #include"finddialog.h"
    4 int main(int argc,char *argv[]){
    5     QApplication app(argc,argv);
    6     FindDialog *dialog = new FindDialog;
    7     dialog->show();
    8     return app.exec();
    9 }
  • 相关阅读:
    宠物店4.0的安装
    《professional asp.net 2.0》读书笔记连载2
    《xhtml 入门系列》之一
    ALinq 让Mysql变得如此简单
    ALinq 入门学习(八)ALinq 对Vs2010 的支持
    教你一款极为简单实用的图表插件
    虚拟机下无法启动 Linux 系统
    怎样去突破文件依赖缓存
    jQuery 表单验证扩展(五)
    Log4Net 全方位跟踪程序运行
  • 原文地址:https://www.cnblogs.com/teng-IT/p/6003267.html
Copyright © 2011-2022 走看看