zoukankan      html  css  js  c++  java
  • Qt学习第二天

    源代码及注释
    头文件:finddialog.h
    
    
    #ifndef FINDDIALOG_H
    #define FINDDIALOG_H
    
    
    #include<QDialog>
    
    
    //一下数行前置声明了一些要用到的类,之所以用前置声明,是因为这样可以编译速度加快
    
    
    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;
    
    
    //子类化QDialog,以下是类的定义
    
    
    class FindDialog:public QDialog
    {
        Q_OBJECT             //因为用到了信号和槽,因此必须包含Q_OBJECT宏定义
    
    
    public:
        FindDialog(QWidget *parent=0);   //构造函数,且指定该类没有父对象
    
    
    //声明了一些信号,Qt
    ::CaseSensitivity cs设置大小写字母的敏感性
    signals:
        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 *caseCheckBox;
        QCheckBox *backwardCheckBox;
        QPushButton *findButton;
        QPushButton *closeButton;
    };
    
    
    #endif // FINDDIALOG_H
    
    
    
    
    实现文件:
    
    
    //QtGui头文件中包含了很多类,基本上只要包含了该头文件,就可以省去很多类的声明
    #include<QtGui>
    #include "finddialog.h"
    
    
    
    
    FindDialog::FindDialog(QWidget *parent)
        :QDialog(parent)
    {
        label=new QLabel(tr("Find &What:"));//&号用来设置快捷键,当按下Alt+W时,就可以选中
        lineEdit=new QLineEdit();
        label->setBuddy(lineEdit);          //设置伙伴关系
    
    
    
    
        caseCheckBox=new QCheckBox(tr("Match &Case"));
        backwardCheckBox=new QCheckBox(tr("Search &Backward"));
    
    
        findButton=new QPushButton(tr("&Find"));
    
    
        findButton->setDefault(true);
        findButton->setEnabled(false);     //使其变灰,即不可以被按
    
    
        closeButton=new QPushButton(tr("Close"));
    //将信号和槽连接
    
    
        connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
        connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
        connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    
    
        //水平布局管理器
        QHBoxLayout *topLeftLayout=new QHBoxLayout;
        topLeftLayout->addWidget(label);
        topLeftLayout->addWidget(lineEdit);
        //垂直布局管理器
    
    
        QVBoxLayout *leftLayout=new QVBoxLayout;
        leftLayout->addLayout(topLeftLayout);
        leftLayout->addWidget(caseCheckBox);
        leftLayout->addWidget(backwardCheckBox);
    
    
        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(tr("Find"));   //设置窗口的标题
        setFixedHeight(sizeHint().height()); //设置窗口的高度,
    sizeHint().height()返回理想的高度
    }
    
    
    void FindDialog::findClicked()
    {
        QString text=lineEdit->text();
        //C++中的三元运算符
        Qt::CaseSensitivity cs=caseCheckBox->isChecked()?Qt::CaseInsensitive:Qt::CaseInsensitive;
        if(backwardCheckBox->isChecked())
        {
            emit findPrevious(text,cs);  //发射信号
        }
        else
        {
            emit findNext(text,cs);
        }
    }
    
    
    void FindDialog::enableFindButton(const QString &text)
    {
        findButton->setEnabled(!text.isEmpty());   //设置findButton的活性
    }
    
    
    
    
    主函数:
    
    
    #include<QApplication>
    #include "finddialog.h"
    
    
    int main(int argc,char *argv[])
    {
        QApplication app(argc,argv);
        FindDialog *dialog=new FindDialog;
        dialog->show();
        return app.exec();
    }
    
    
  • 相关阅读:
    Mybatis oracle多表联合查询分页数据重复的问题
    The Decade of Deep Learning
    D3S – A Discriminative Single Shot Segmentation Tracker
    Recent Advancements in NLP
    A list of datasets directly related to Music Information Retrieval Datasets
    Best Resources for Imbalanced Classification
    Attention-based Dropout Layer for Weakly Supervised Object Localization
    Learning a Discriminative Filter Bank within a CNN for Fine-grained Recognition
    Batch DropBlock Network for Person Re-identification and Beyond
    Cross-channel Communication Networks
  • 原文地址:https://www.cnblogs.com/phisy/p/3373611.html
Copyright © 2011-2022 走看看