Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. [Solved] 对《C++ GUI Programming with Qt 4 (2nd Edition)》书上例子的疑惑
Forum Updated to NodeBB v4.3 + New Features

[Solved] 对《C++ GUI Programming with Qt 4 (2nd Edition)》书上例子的疑惑

Scheduled Pinned Locked Moved Chinese
3 Posts 2 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nimingzhe2008
    wrote on last edited by
    #1

    在这本书的第二章,给了一个例子
    头文件finddialog.h
    @#ifndef FINDDIALOG_H
    #define FINDDIALOG_H

    #include <QDialog>

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

    class FindDialog : public QDialog
    {
    Q_OBJECT

    public:
    FindDialog(QWidget *parent = 0);

    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.cpp的部分代码
    @#include <QtGui>

    #include "finddialog.h"

    FindDialog::FindDialog(QWidget *parent)//疑问一:parent由始至终没有使用
    : QDialog(parent)
    {
    label = new QLabel(tr("Find &what:"));
    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);//疑问2:为什么不是“对象.setLayout(mainLayout);”的形式
    
    setWindowTitle(tr("Find"));//疑问同上
    setFixedHeight(sizeHint().height());//疑问同上
    

    }@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      建议先补补C++的课再深入Qt吧。

      1. 下一行紧接着不就用了么?
        @
        FindDialog::FindDialog(QWidget *parent)//疑问一:parent由始至终没有使用
        : QDialog(parent)
        @

      2 因为this可以省略
      @
      setLayout(mainLayout);//疑问2:为什么不是“对象.setLayout(mainLayout);”的形式

      setWindowTitle(tr("Find"));//疑问同上
      setFixedHeight(sizeHint().height());//疑问同上
      

      @

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nimingzhe2008
        wrote on last edited by
        #3

        谢谢。

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved