如果需要讓一個窗口支持模態和非模態窗口,一般是需要將改窗口設置爲QDailog,
實現該功能的一個關鍵點是設置了setStyleSheet(Qt::Window),源碼如下:
Widget = 0x00000000,
Window = 0x00000001,
Dialog = 0x00000002 | Window,
Qt::Dialog |
0x00000002 | Window |
Indicates that the widget is a window that should be decorated as a dialog (i.e., typically no maximize or minimize buttons in the title bar). This is the default type for QDialog. If you want to use it as a modal dialog, it should be launched from another window, or have a parent and used with the QWidget::windowModality property. If you make it modal, the dialog will prevent other top-level windows in the application from getting any input. We refer to a top-level window that has a parent as a secondary window. |
確實可以使用QDialog實現模態和非模態對話框的功能,還有其他方法實現類似的功能嗎,可以使用QWidget實現類似的效果嗎,可以使用QWidget實現窗口置頂且爲非模態對話框的功能,如果需求不要求是模態對話框,只需要窗口置頂和非模態窗口,可以使用QDialog實現類似的功能,也可以使用QWidget實現,QWidget本身默認是沒有這樣的功能的需要多做一些設置就可以實現了,可以設置QWidget::setWindowsFlags(this->windowsFlags() | Qt::Window),就可以有這樣的功能。
設置了Qt::Window標誌之後,窗口就具有一直置頂的功能,且爲非模態對話框的功能。所以如果需求是這樣的話,既可以使用QDialog實現,也可以使用QWindow實現,設置Qt::Window就可以了,不需要再設置窗口一直置頂等類似的windowFlags也可以實現非模態對話框且窗口置頂的功能。當然如果需求要求是模態對話框,建議還是用現成封裝好的QDialog就可以了。
如果需要實現模態對話框功能,但是窗口效果確實非模態的,可以使用QWidget,QWidget::setWindowsFlags(this->windowsFlags() | Qt::Window),就可以實現模態對話框的窗口效果,再加上使用
show();
QEventLoop evtLoop;
m_pEvtLoop = &evtLoop;
evtLoop.exec();
然後再彈出的非模態窗口的自定義確定取消按鈕做如下操作就可以了
void ColorDialog::okBtnClickedSlot()
{
m_buttonRole = Yes;
if (m_pEvtLoop != NULL)
{
m_pEvtLoop->exit();
}
this->hide();
}
void ColorDialog::cancelBtnClickedSlot()
{
m_buttonRole = No;
if (m_pEvtLoop != NULL)
{
m_pEvtLoop->exit();
}
this->hide();
}
Qt QGridLayout相關的bug
在使用Qt的網格佈局的時候,可以將各種數據比如QWidget表格順序整齊的排列好,但是在具體使用中,發現QGridLayout並沒有達到預期的對齊和顯示效果,應該是Qt底層內核相關的一個bug,具體如下,如果用QGridLayout,做出一個顏色樣板
仔細看會發現有些間隔顯得有點寬,有些間隔顯得有點窄,具體代碼如下
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setContentsMargins(0, 0, 0, 0);
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setStyleSheet("border:0px;");
void BasicColorItem::paintEvent(QPaintEvent *ev)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(m_color);
painter.drawRect(0, 0, width(), height());
}
該做的設置應該多是做了的,還是做不到間隔均衡顯示,如不把item背景顏色換爲相同就可以了,這可能應該和qt相關的內核或者這方面的底層渲染沒有弄好,所以說這個應該可以說是qt的一個bug,所以如果不在意這麼一點瑕疵可以就使用這種方式就可以了,如果需要解決這樣的bug,可以考慮使用QTableWidget等類似的組件代替使用QGridLayout。