Qt:亲手写框体 C++ GUI   (没课的…

本文详细介绍了如何使用C++和Qt库创建一个简单的查找对话框,包括对话框的基本组件、信号与槽机制的应用以及对话框功能的实现。通过阅读本文,读者可以了解如何构建具有查找功能的用户界面。

 

以下代码是 finddialog.h  :

#include <QDialog>

//  定义需要的信号和槽.

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
 Q_OBJECT       //QT宏定义必须.

public:
      FindDialog(QWidget *parent = 0);
signals:              //Find按钮发出的两个信号. singnals是一个宏.
      void findNext(const QString &str , Qt::CaseSensitivity cs);
      void findPrevious(const QString &str , Qt::CaseSensitivity cs);

  //private声明的两个槽.
private slots:
      void findClicked();
      void enableFindButton(const QString &text);
private:
      QLabel *label;
      QLineEdit *lineEdit;
      QCheckBox *caseCheckBox;
      QCheckBox *backwardCheckBox;
      QPushButton *findButton;
      QPushButton *closeButton; 
};

 

以下是 finddialog.cpp :

#include <QtGui>
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
      label = new QLabel(tr("Find &what:"));   // ‘&’ 表示在框体w是快捷键.   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);          //Find成为对话框的默认按钮.
      findButton->setEnabled(false);       //Find变成禁用按钮,为灰色,不能进行交互.

      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()) );

//Qt中用QHBoxLayout , QVBoxLayout , QGridLayout 三个布局的不同嵌套来构造复杂的对话框.

      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());    //默认一个“理想”的大小.
}

void FindDialog::findClicked()
{
      QString text = lineEdit->text();
      Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
 
      if(backwardCheckBox->isChecked())
     {
            emit findPrevious(text , cs);  //emit是Qt中的关键字 会被转化为C++的预处理成为C++代码.
     }
     else
     {
            emit findNext(text , cs);
     }
}

void FindDialog::enableFindButton(const QString &text)
{
      findButton->setEnabled(!text.isEmpty());
}

 

接下来是 main.cpp 主函数:

#include <QApplication>
#include "finddialog.h"

int main(int argc , char *argv[])
{
      QApplication app(argc,argv);
      FindDialog *dialog = new FindDialog;
      dialog->show();
      return app.exec();
}

 

(代码在同一个目录下)

编译过程:

              1.  qmake -project

              2.  qmake       (生成Makefile 文件)

              3.  make         (编译)

              4.  ./(执行文件名称)

结果如图:

Qt:亲手写框体 <wbr>C++ <wbr>GUI <wbr> <wbr> <wbr>(没课的日子-。-!乱来一下!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值