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



  • 在这本书的第二章,给了一个例子
    头文件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());//疑问同上
    

    }@



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

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

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

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

    @



  • 谢谢。


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.