翻譯|使用教程|編輯:龔雪|2024-11-07 11:10:04.340|閱讀 105 次
概述:本文主要介紹展示如何使用Qt中可用的窗口標志,歡迎下載最新版組件體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
窗口標志要么是類型,要么是提示。類型用于為小部件指定各種窗口系統屬性,一個小部件只能有一種類型,默認是,但是小部件可以有零個或多個提示;提示用于自定義頂級窗口的外觀。
小部件的標志存儲在Qt::WindowFlags類型中,該類型存儲標志的OR組合。
Qt技術交流群:166830288 歡迎一起進群討論
這個例子由兩個類組成:
我們將從回顧ControllerWindow類開始,然后再看看PreviewWindow類。
class ControllerWindow : public QWidget { Q_OBJECT public: ControllerWindow(QWidget *parent = nullptr); private slots: void updatePreview(); private: void createTypeGroupBox(); void createHintsGroupBox(); QCheckBox *createCheckBox(const QString &text); QRadioButton *createRadioButton(const QString &text); PreviewWindow *previewWindow; QGroupBox *typeGroupBox; QGroupBox *hintsGroupBox; QPushButton *quitButton; QRadioButton *windowRadioButton; QRadioButton *dialogRadioButton; QRadioButton *sheetRadioButton; QRadioButton *drawerRadioButton; QRadioButton *popupRadioButton; QRadioButton *toolRadioButton; QRadioButton *toolTipRadioButton; QRadioButton *splashScreenRadioButton; QCheckBox *msWindowsFixedSizeDialogCheckBox; QCheckBox *x11BypassWindowManagerCheckBox; QCheckBox *framelessWindowCheckBox; QCheckBox *windowNoShadowCheckBox; QCheckBox *windowTitleCheckBox; QCheckBox *windowSystemMenuCheckBox; QCheckBox *windowMinimizeButtonCheckBox; QCheckBox *windowMaximizeButtonCheckBox; QCheckBox *windowCloseButtonCheckBox; QCheckBox *windowContextHelpButtonCheckBox; QCheckBox *windowShadeButtonCheckBox; QCheckBox *windowStaysOnTopCheckBox; QCheckBox *windowStaysOnBottomCheckBox; QCheckBox *customizeWindowHintCheckBox; };
ControllerWindow類繼承了QWidget,該小部件允許用戶在可用的窗口標志中進行選擇,并在單獨的預覽窗口中顯示效果。
我們聲明了一個私有updatePreview()槽,以便在用戶更改窗口標志時刷新預覽窗口。
我們還聲明了幾個私有函數來簡化構造函數:調用createTypeGroupBox()函數,使用私有createButton()函數為每個可用的窗口類型創建一個單選按鈕,并將它們收集到一個組框中。以類似的方式,我們使用createHintsGroupBox()函數為每個可用的提示創建一個復選框,使用私有的createCheckBox()函數。
除了各種單選按鈕和復選框之外,我們還需要一個相關的PreviewWindow來顯示當前選擇的窗口標志的效果。
ControllerWindow::ControllerWindow(QWidget *parent) : QWidget(parent) { previewWindow = new PreviewWindow(this); createTypeGroupBox(); createHintsGroupBox(); quitButton = new QPushButton(tr("&Quit")); connect(quitButton, &QPushButton::clicked, qApp, &QCoreApplication::quit); QHBoxLayout *bottomLayout = new QHBoxLayout; bottomLayout->addStretch(); bottomLayout->addWidget(quitButton); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(typeGroupBox); mainLayout->addWidget(hintsGroupBox); mainLayout->addLayout(bottomLayout); setLayout(mainLayout); setWindowTitle(tr("Window Flags")); updatePreview(); }
在構造函數中,我們首先創建預覽窗口。然后使用私有的createTypeGroupBox()和createHintsGroupBox()函數創建包含可用窗口標志的組框。此外,我們還創建了一個Quit按鈕,將按鈕和一個可伸縮空間放在一個單獨的布局中,來使按鈕出現在WindowFlag小部件的右下角。
最后,我們將按鈕的布局和兩個組框添加到QVBoxLayout中,設置窗口標題并使用updatePreview()槽刷新預覽窗口。
void ControllerWindow::updatePreview() { Qt::WindowFlags flags; if (windowRadioButton->isChecked()) flags = Qt::Window; else if (dialogRadioButton->isChecked()) flags = Qt::Dialog; else if (sheetRadioButton->isChecked()) flags = Qt::Sheet; else if (drawerRadioButton->isChecked()) flags = Qt::Drawer; else if (popupRadioButton->isChecked()) flags = Qt::Popup; else if (toolRadioButton->isChecked()) flags = Qt::Tool; else if (toolTipRadioButton->isChecked()) flags = Qt::ToolTip; else if (splashScreenRadioButton->isChecked()) flags = Qt::SplashScreen;
每當用戶更改任何窗口標志時,就調用updatePreview()插槽。首先我們創建一個空Qt::WindowFlags標志,然后確定要檢查的類型并將其添加到標志中。
if (msWindowsFixedSizeDialogCheckBox->isChecked()) flags |= Qt::MSWindowsFixedSizeDialogHint; if (x11BypassWindowManagerCheckBox->isChecked()) flags |= Qt::X11BypassWindowManagerHint; if (framelessWindowCheckBox->isChecked()) flags |= Qt::FramelessWindowHint; if (windowNoShadowCheckBox->isChecked()) flags |= Qt::NoDropShadowWindowHint; if (windowTitleCheckBox->isChecked()) flags |= Qt::WindowTitleHint; if (windowSystemMenuCheckBox->isChecked()) flags |= Qt::WindowSystemMenuHint; if (windowMinimizeButtonCheckBox->isChecked()) flags |= Qt::WindowMinimizeButtonHint; if (windowMaximizeButtonCheckBox->isChecked()) flags |= Qt::WindowMaximizeButtonHint; if (windowCloseButtonCheckBox->isChecked()) flags |= Qt::WindowCloseButtonHint; if (windowContextHelpButtonCheckBox->isChecked()) flags |= Qt::WindowContextHelpButtonHint; if (windowShadeButtonCheckBox->isChecked()) flags |= Qt::WindowShadeButtonHint; if (windowStaysOnTopCheckBox->isChecked()) flags |= Qt::WindowStaysOnTopHint; if (windowStaysOnBottomCheckBox->isChecked()) flags |= Qt::WindowStaysOnBottomHint; if (customizeWindowHintCheckBox->isChecked()) flags |= Qt::CustomizeWindowHint; previewWindow->setWindowFlags(flags);
我們還確定哪些暗示的檢查,并將它們添加到標記使用的OR操作符,使用標志來設置預覽窗口的窗口標志。
QPoint pos = previewWindow->pos(); if (pos.x() < 0) pos.setX(0); if (pos.y() < 0) pos.setY(0); previewWindow->move(pos); previewWindow->show(); }
我們調整預覽窗口的位置,這樣做的原因是在某些平臺上,擺弄窗戶的框架可能會導致窗戶的位置在背后發生變化。如果窗口位于屏幕的左上角,則可能看不到該窗口的某些部分。因此我們調整小部件的位置,以確保如果發生這種情況,窗口在屏幕邊界內移動,最后調用 QWidget::show() 來確保預覽窗口可見。
void ControllerWindow::createTypeGroupBox() { typeGroupBox = new QGroupBox(tr("Type")); windowRadioButton = createRadioButton(tr("Window")); dialogRadioButton = createRadioButton(tr("Dialog")); sheetRadioButton = createRadioButton(tr("Sheet")); drawerRadioButton = createRadioButton(tr("Drawer")); popupRadioButton = createRadioButton(tr("Popup")); toolRadioButton = createRadioButton(tr("Tool")); toolTipRadioButton = createRadioButton(tr("Tooltip")); splashScreenRadioButton = createRadioButton(tr("Splash screen")); windowRadioButton->setChecked(true); QGridLayout *layout = new QGridLayout; layout->addWidget(windowRadioButton, 0, 0); layout->addWidget(dialogRadioButton, 1, 0); layout->addWidget(sheetRadioButton, 2, 0); layout->addWidget(drawerRadioButton, 3, 0); layout->addWidget(popupRadioButton, 0, 1); layout->addWidget(toolRadioButton, 1, 1); layout->addWidget(toolTipRadioButton, 2, 1); layout->addWidget(splashScreenRadioButton, 3, 1); typeGroupBox->setLayout(layout); }
私有的createTypeGroupBox()函數從構造函數中調用。
首先我們創建一個組框,然后為窗口標志中的每個可用類型創建一個單選按鈕(使用私有的createRadioButton() 函數),我們將Qt::Window作為最初應用的類型,將單選按鈕放入中,并將布局安裝在組框上。
我們不包含默認的Qt::Widget類型,原因是它的行為與其他類型有些不同。如果沒有為小部件指定類型,并且它沒有父部件,則該小部件是窗口。但是如果它有父部件,它就是標準的子部件。其他類型都是頂級窗口,由于提示只影響頂級窗口,我們放棄Qt::Widget類型。
void ControllerWindow::createHintsGroupBox() { hintsGroupBox = new QGroupBox(tr("Hints")); msWindowsFixedSizeDialogCheckBox = createCheckBox(tr("MS Windows fixed size dialog")); x11BypassWindowManagerCheckBox = createCheckBox(tr("X11 bypass window manager")); framelessWindowCheckBox = createCheckBox(tr("Frameless window")); windowNoShadowCheckBox = createCheckBox(tr("No drop shadow")); windowTitleCheckBox = createCheckBox(tr("Window title")); windowSystemMenuCheckBox = createCheckBox(tr("Window system menu")); windowMinimizeButtonCheckBox = createCheckBox(tr("Window minimize button")); windowMaximizeButtonCheckBox = createCheckBox(tr("Window maximize button")); windowCloseButtonCheckBox = createCheckBox(tr("Window close button")); windowContextHelpButtonCheckBox = createCheckBox(tr("Window context help button")); windowShadeButtonCheckBox = createCheckBox(tr("Window shade button")); windowStaysOnTopCheckBox = createCheckBox(tr("Window stays on top")); windowStaysOnBottomCheckBox = createCheckBox(tr("Window stays on bottom")); customizeWindowHintCheckBox= createCheckBox(tr("Customize window")); QGridLayout *layout = new QGridLayout; layout->addWidget(msWindowsFixedSizeDialogCheckBox, 0, 0); layout->addWidget(x11BypassWindowManagerCheckBox, 1, 0); layout->addWidget(framelessWindowCheckBox, 2, 0); layout->addWidget(windowNoShadowCheckBox, 3, 0); layout->addWidget(windowTitleCheckBox, 4, 0); layout->addWidget(windowSystemMenuCheckBox, 5, 0); layout->addWidget(customizeWindowHintCheckBox, 6, 0); layout->addWidget(windowMinimizeButtonCheckBox, 0, 1); layout->addWidget(windowMaximizeButtonCheckBox, 1, 1); layout->addWidget(windowCloseButtonCheckBox, 2, 1); layout->addWidget(windowContextHelpButtonCheckBox, 3, 1); layout->addWidget(windowShadeButtonCheckBox, 4, 1); layout->addWidget(windowStaysOnTopCheckBox, 5, 1); layout->addWidget(windowStaysOnBottomCheckBox, 6, 1); hintsGroupBox->setLayout(layout); }
私有的createHintsGroupBox()函數也從構造函數中調用。
同樣我們要做的第一件事是創建一個組框,然后使用私有的createCheckBox()函數為窗口標志中的每個可用提示創建一個復選框。我們將復選框放入QGridLayout中,并在組框上安裝布局。
QCheckBox *ControllerWindow::createCheckBox(const QString &text) { QCheckBox *checkBox = new QCheckBox(text); connect(checkBox, &QCheckBox::clicked, this, &ControllerWindow::updatePreview); return checkBox; }
私有的createCheckBox()函數從createHintsGroupBox()調用。
我們只需用提供的文本創建一個QCheckBox,將其連接到私有的updatePreview()槽,并返回一個指向復選框的指針。
QRadioButton *ControllerWindow::createRadioButton(const QString &text) { QRadioButton *button = new QRadioButton(text); connect(button, &QRadioButton::clicked, this, &ControllerWindow::updatePreview); return button; }
在私有的createRadioButton()函數中,它是我們用提供的文本創建的QRadioButton,并連接到私有的updatePreview()槽。該函數從createTypeGroupBox()調用,并返回一個指向按鈕的指針。
未完待續,精彩下期繼續......
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網