Summary of QT knowledge points (1)

QT download address

1. Set the focus order of the tab key:

static void QWidget::setTabOrder(QWidget * first, QWidget * second);

If you don't want a sub-component to be focused, you can set the following for the sub-component:

setFocusPolicy(Qt::NoFocus);

2. There is no right-click menu for setting editing:

setContextMenuPolicy(Qt::NoContextMenu);

3. If you want to monitor some child controls in the dialog box, you can install an event filter for each child control in the dialog box's constructor, and then make a judgment in the implementation of the eventFilter function of the dialog box. Note: 1. If the specified event is filtered and the child control does not need to be notified, it must return true, otherwise it returns false. 2. If you delete the receiving object in the eventFilter() function, you must return true. If it returns false, Qt will send an event to the deleted object and the program will crash. 3. The dialog box and the child control must be in the same thread, otherwise, the filter will not be called.

CustomerInfoDialog::CustomerInfoDialog(QWidget *parent)
    : QDialog(parent)
{
    firstNameEdit->installEventFilter(this);
    lastNameEdit->installEventFilter(this);
    cityEdit->installEventFilter(this);
    phoneNumberEdit->installEventFilter(this);
}

4. Q_UNSED (object), just to eliminate the compilation warning that the variable is not used

5. QKeySequence is a key series class. It is usually used to determine some commonly used responses. For example, in event filtering, the bool QKeyEvent::matches( QKeySequence::StandardKey key) const function of QKeyEvent is used to determine the current key combination status. There are: QKeySequence::SelectAll, QKeySequence::Copy, QKeySequence::Paste, etc., or you can construct it to judge by yourself , for example: QKeySequence (Qt::CTRL + Qt::Key_P); or QKeySequence (tr("Ctrl+p "));

6. Realization of software restart:

void Widget::reboot()
{
    QString program = QApplication::applicationFilePath();
    QStringList arguments = QApplication::arguments();
    QString workingDirectory = QDir::currentPath();
    //startDetached()将已分离的方式启动一个新进程
    QProcess::startDetached(program, arguments, workingDirectory);
    QApplication::exit();
}

7. The difference between MSVC and MinGW:

  • MSVC refers to Microsoft's VC compiler
  • MinGW stands for Minimalist GNU on Windows. It is a freely useable and freely distributed collection of Windows specific header files and import libraries using the GNU toolset, allowing you to generate native Windows programs on GNU/Linux and Windows platforms without the need for third-party C runtime libraries.
     

8. QT finds the sub-control function:

1、T QObject::findChild(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively)
2、QList<T> QObject::findChildren(const QString &name=QString()) const
例子:
QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
QList<QLineEdit *> lineEdit = tabWidget.findChildren<QLineEdit *>("lineEdit");

9. There is a QSizeGrip (prompt window stretching flag) in the lower right corner of QDialog and QMainWindow by default, use setSizeGripEnabled() to enable it

10. QDialog dialog box type:

  1. Modal dialog box: The entire application window cannot accept user responses, and is in a waiting state, and the following code cannot be executed.
  2. Non-modal dialog box: The user can still operate on other windows, and will not be unable to operate other windows because this dialog box is not closed.
  3. Semi-modal dialog box: Block the response of the window, but will not affect the execution of subsequent code. Call setModal(true) or setWindowModality(), then show().

11. QWidget provides the setWindowModality() method to set the window semi-modal or non-modal:

  • Qt::NonModal: Non-modal dialog box (default value)
  • Qt::WindowModal: only blocks the parent window, the parent window of the parent window, and the sibling windows. (Semi-modal dialog box)
  • Qt::ApplicationModal: Block all windows of the entire application. (Semi-modal dialog box)

12. The open() method of QDialog is to display a semi-modal dialog box, show() is to display the dialog box, and does not care about the dialog box type, exec() displays the modal dialog box and blocks the entire program, that is, the dialog box is internally displayed. The property is set to: Qt::ApplicationModal

13. QLabel sets the text alignment:

  • 1. Member function: setAlignment(Qt::AlignCenter);
  • 2. Style sheet: setStyleSheet("qproperty-alignment:'AlignBottom | AlignRight';");

14. When QLabel sets the text to be too long, it will automatically wrap: setWordWrap(true); the text needs to contain spaces.

Under normal circumstances, the text will be closer between the upper and lower lines after automatic line wrapping. We can set the line height in the following way.

pLabel->setWordWrap(true);
QString strText = QStringLiteral("一去二三里,烟村四五家。亭台六七座,八九十枝花。");
QString strHeightText = "<p style=\"line-height:%1%\">%2<p>";
strText = strHeightText.arg(150).arg(strText);
pLabel->setText(strText);

15. If the QLabel is too long and we don't want to wrap, but only want to omit part of it as..., then we can achieve it through QFontMetrics:

QString strText = QStringLiteral("一去二三里,烟村四五家。亭台六七座,八九十枝花。");
QString strElidedText = pLabel->fontMetrics().elidedText(strText, Qt::ElideRight, 200, Qt::TextShowMnemonic);
pLabel->setText(strElidedText);

16. To set multiple buttons to be mutually exclusive, you can first set several buttons to setCheckable(true) to indicate that they can be selected, and then put all of them in QButtonGroup, set setExclusive(true) to indicate that their child buttons are mutually exclusive, through the associated signal buttonClicked(QAbstractButton*), know which button was pressed

17. QCheckBox uses setTristate(true) to enable its tri-state function

18. QLineEdit has a frame specified by the platform style guide by default, which can be closed by setFrame(false).

19. QLineEdit can add widgets through the addAction function and specify their display position

QAction * addAction(const QIcon & icon, ActionPosition position)

20. QLineEdit can set the validator (setValidator()) or the input mask (setInputmask())

21. Both QSpinBox and QDoubleSpinBox are derived from QAbstractSpinBox. Common methods:

pSpinBox->setRange(20, 200);  // 范围
pSpinBox->setSingleStep(10); // 步长
pSpinBox->setValue(150);  // 当前值
pSpinBox->setPrefix("$ ");  // 前缀
pSpinBox->setSuffix(" %");  // 后缀
pSpinBox->setWrapping(true);  // 开启循环
pSpinBox->setSpecialValueText(tr("Automatic"));  // 特殊文本值即默认值

If using prefix(), suffix() and specialValueText() does not provide enough control, you can subclass QSpinBox and override valueFromText() and textFromValue().

21. QProgressBar-If both the minimum and maximum values ​​are set to 0, the progress bar will display a busy indicator instead of the current value. setInvertedAppearance() is used to set the direction of progress of the progress bar. setTextVisible() sets whether the text on the progress bar is displayed. setFormat() sets the text display format.

22, QDateTimeEdit-setCalendarPopup(true) set to add calendar popup

23. QScrollArea assigns a scroll bar to a control through the member function setWidget(). You can use the widget() function to get the widget, and the view can use the setWidgetResizable() function to adjust the size. Use the takeWidget() function to remove the widget from the scrolling area. When removing it, the ownership of the widget will be passed to the caller, which can be received by QWidget.

24. The QToolBox class provides a column (tabbed) widget item. Use addItem() to add Item, or insert it at a specific position through insertItem().

25. The QSystemTrayIcon class provides an icon in the system tray for the application.

26. The QFileSystemWatcher class is used to provide an interface for monitoring file and directory modification.

27. The functions provided by the QDesktopServices class are used to access common desktop services. The commonly used static member functions are as follows:

bool openUrl(const QUrl & url);//如果成功,返回true;否则,返回false。
//1、如果是网络URL,则以用户桌面环境的适当Web浏览器打开指定的的url。
//2、如果是本地文件,将会使用默认程序打开,例如.txt使用notepad
//3、如果是文件夹,则使用window直接打开
//4、如果指定一个mail地址,将会打开e-mail客户端(例如:Outlook)
//例如,下面的URL包含收件人([email protected])、主题(Qt)和正文(I am a Qter):
//QString strUrl = QString("mailto:%1?subject=%2&//body=%3").arg("[email protected]").arg("Qt").arg("I am a Qter");
//QDesktopServices::openUrl(QUrl(strUrl));
//为给定的scheme设置handler,receiver是接受者,method为receiver提供的处理函数。
void setUrlHandler(const QString & scheme, QObject * receiver, const char * method)
//例如:QDesktopServices::setUrlHandler("http", this, "openUrl_wmm");
//表示所有使用QDesktopServices::openUrl()处理的http形式的url都不在使用默认的web浏览器打开,而是直//接调用你的成员函数openUrl_wmm(),注意此函数只有一个参数(即const QUrl &url),且必须为公有槽函数
//删除以上面方式指定的scheme预先设定的URL handler。
void unsetUrlHandler(const QString & scheme)

28. QTimer can set the type of timer, the default is: Qt::CoarseTimer

void setTimerType(Qt::TimerType atype)


If you call the start() function of QTimer directly without setting an interval, the timer will continuously send out the timeout signal when there is no event.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/102888785