C ++ GUI Programming with Qt4 notes

  Take finddialog as an example to introduce QDialog. And made a further introduction to the basic concepts and skills of Qt (chap1).
  
  1. MOC extension and signals–slots mechanism
  
  Strictly speaking, Qt development is not written in standard C ++.
  
  Meta-Object System is an important extension of Qt to C ++, referred to as moc.
  
  moc is a mechanism for creating independent software components
  
  that can be bound together without any component knowing anything about the other components it is connected to
  
  provide 2 key contents: signals–slots and introspection.
  
  Among them, introspection is the basis for implementing signals–slots.
  
  The mechanism works as follows:
  
  The Q_OBJECT macro declares some introspection functions that must be implemented in every QObject subclass: metaObject (), tr (), qt_metacall (), and a few more.
  
  Qt's moc tool generates implementations for the functions declared by Q_OBJECT and for all the signals.
  
  QObject member functions such as connect () and disconnect () use the introspection functions to do their work.
  
  All of this is handled automatically by qmake, moc, and QObject, so you rarely need to think about it.
  
  But if you are curious, you can read the QMetaObject class documentation
  
  and have a look at the C ++ source files generated by moc to see how the implementation works.
  
  2. Tips and details
  
  2.1 class forward declaration
  
  In the header file, if only a pointer to a class is used, it does not depend on the definition and implementation of the class.
  
  You can use class ClassName to declare a class, instead of #include "ClassName.h"
  
  Compared to loading header files, the predeclaration is more concise and has better performance.
  
  2.2 Internationalization: user-visible text, use tr ()
  
  2.3 Shortcut key: use & to declare shortcut key.
  
  For example, & Find declares the shortcut key to be F,
  
  setBuddy (widget). When the shortcut key is pressed, the focus is on the widget component, not itself.
  
  2.4 setDefault Behavior when the Enter key is pressed.
  
  2.5 SetLayout will recalculate the parent-child relationship.
  
  All components in the layout become children of the setLayout object.
  
  2.6 When using moc mechanism (Q_OBJECT), the class definition must be in the .h file, otherwise moc will not work properly.
  
  bubuko.com, cloth button
  
  // 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
  
  bubuko.com,布布扣
  
  bubuko.com,布布扣
  
  // finddialog.cpp
  
  #include <QtGui>
  
  #include "finddialog.h"
  
  FindDialog::FindDialog(QWidget *parent)
  
  : QDialog(parent)
  
  {
  
  // widgets
  
  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"));
  
  // connections
  
  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()));
  
  // layout
  
  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);
  
  } else {
  
  emit findNext(text, cs);
  
  }
  
  }
  
  void FindDialog::enableFindButton(const QString &text)
  
  {
  
  findButton->setEnabled(!text.isEmpty());
  
  }
  
  bubuko.com,布布扣
  
  bubuko.com,布布扣
  
  // 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();
  
  }

Guess you like

Origin www.cnblogs.com/aquariusunny/p/12729787.html