April 29, 2012

nimingzhe2008 nimingzhe2008
Ant Farmer
60 posts

[Solved] questions about an example of “C++ GUI Programming with Qt 4 (2nd Edition)”

 

There is an example in chapter 2 of the book
finddialog.h

  1. #ifndef FINDDIALOG_H
  2. #define FINDDIALOG_H
  3.  
  4. #include <QDialog>
  5.  
  6. class QCheckBox;
  7. class QLabel;
  8. class QLineEdit;
  9. class QPushButton;
  10.  
  11. class FindDialog : public QDialog
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     FindDialog(QWidget *parent = 0);
  17.  
  18. signals:
  19.     void findNext(const QString &str, Qt::CaseSensitivity cs);
  20.     void findPrevious(const QString &str, Qt::CaseSensitivity cs);
  21.  
  22. private slots:
  23.     void findClicked();
  24.     void enableFindButton(const QString &text);
  25.  
  26. private:
  27.     QLabel *label;
  28.     QLineEdit *lineEdit;
  29.     QCheckBox *caseCheckBox;
  30.     QCheckBox *backwardCheckBox;
  31.     QPushButton *findButton;
  32.     QPushButton *closeButton;
  33. };
  34.  
  35. #endif

part of finddialog.cpp
  1. #include <QtGui>
  2.  
  3. #include "finddialog.h"
  4.  
  5. FindDialog::FindDialog(QWidget *parent)//Why is parent never used?
  6.     : QDialog(parent)
  7. {
  8.     label = new QLabel(tr("Find &what:"));
  9.     lineEdit = new QLineEdit;
  10.     label->setBuddy(lineEdit);
  11.  
  12.     caseCheckBox = new QCheckBox(tr("Match &case"));
  13.     backwardCheckBox = new QCheckBox(tr("Search &backward"));
  14.  
  15.     findButton = new QPushButton(tr("&Find"));
  16.     findButton->setDefault(true);
  17.     findButton->setEnabled(false);
  18.  
  19.     closeButton = new QPushButton(tr("Close"));
  20.  
  21.     connect(lineEdit, SIGNAL(textChanged(const QString &)),
  22.             this, SLOT(enableFindButton(const QString &)));
  23.     connect(findButton, SIGNAL(clicked()),
  24.             this, SLOT(findClicked()));
  25.     connect(closeButton, SIGNAL(clicked()),
  26.             this, SLOT(close()));
  27.  
  28.     QHBoxLayout *topLeftLayout = new QHBoxLayout;
  29.     topLeftLayout->addWidget(label);
  30.     topLeftLayout->addWidget(lineEdit);
  31.  
  32.     QVBoxLayout *leftLayout = new QVBoxLayout;
  33.     leftLayout->addLayout(topLeftLayout);
  34.     leftLayout->addWidget(caseCheckBox);
  35.     leftLayout->addWidget(backwardCheckBox);
  36.  
  37.     QVBoxLayout *rightLayout = new QVBoxLayout;
  38.     rightLayout->addWidget(findButton);
  39.     rightLayout->addWidget(closeButton);
  40.     rightLayout->addStretch();
  41.  
  42.     QHBoxLayout *mainLayout = new QHBoxLayout;
  43.     mainLayout->addLayout(leftLayout);
  44.     mainLayout->addLayout(rightLayout);
  45.     setLayout(mainLayout);//question:Why isn't it in the way of "object. setLayout(mainLayout); "? And which is object?
  46.  
  47.     setWindowTitle(tr("Find"));//same question
  48.     setFixedHeight(sizeHint().height());//same question
  49. }

2 replies

April 29, 2012

gmaro gmaro
Lab Rat
50 posts

  1.     setLayout(mainLayout);//question:Why isn't it in the way of "object. setLayout(mainLayout); "? And which is object?
  2.  
  3.     setWindowTitle(tr("Find"));//same question
  4.     setFixedHeight(sizeHint().height());//same question

You’re applying layout/title/height to object itself in constructor. This is the same as:
  1.     this->setLayout(mainLayout);
  2.     this->setWindowTitle(tr("Find"));
  3.     this->setFixedHeight(sizeHint().height());

You should read some C++ book of C++ FAQ about “this”.

  1. FindDialog::FindDialog(QWidget *parent)//Why is parent never used?

Technically it is used in init list
  1.     : QDialog(parent)

There is not alway necessary to use parent pointer, as you can see in .h file it is 0 by default. Book that you’re reading explains QObject parentship pretty well.

April 30, 2012

nimingzhe2008 nimingzhe2008
Ant Farmer
60 posts

Thank you,I understand.

 
  ‹‹ QSystemDeviceInfo in Qt Desktop application      Double the column in QSqlTableModel ››

You must log in to post a reply. Not a member yet? Register here!