ui.serviceType->setFixedWidth(95);
m_mainMenu = new QMenu(this);
m_osSubMenu = new QMenu(this);
m_appSubMenu = new QMenu(this);
m_details = new QAction(QStringLiteral("Details"),this);
m_details->setCheckable(true);
m_details->setChecked(true);
m_settings = new QAction(QStringLiteral("Settings"), this);
m_settings->setIcon(QIcon(":/misc/preference"));
m_settings->setShortcut(QKeySequence::Print);
m_os = new QAction(QStringLiteral("OS"), this);
m_app = new QAction(QStringLiteral("Applications"), this);
m_github = new QAction(QStringLiteral("Github"), this);
m_github->setIcon(QIcon(":/app/github"));
m_github->setShortcut(QKeySequence("Ctrl+G"));
m_amazon = new QAction(QStringLiteral("Amazon"), this);
m_amazon->setIcon(QIcon(":/app/amazon"));
m_photoshop = new QAction(QStringLiteral("Photoshop"), this);
m_photoshop->setIcon(QIcon(":/app/photoshop"));
m_facebook = new QAction(QStringLiteral("Facebook"), this);
m_facebook->setIcon(QIcon(":/app/facebook"));
m_apple = new QAction(QStringLiteral("Apple"), this);
m_apple->setShortcut(QKeySequence("Ctrl+A"));
m_apple->setIcon(QIcon(":/os/apple"));
m_windows = new QAction(QStringLiteral("Windows"), this);
m_windows->setIcon(QIcon(":/os/windows"));
m_windows->setShortcut(QKeySequence("Ctrl+W"));
m_fedora = new QAction(QStringLiteral("Fedora"), this);
m_fedora->setIcon(QIcon(":/os/fedora"));
m_fedora->setDisabled(true);
m_osSubMenu->addAction(m_apple);
m_osSubMenu->addAction(m_windows);
m_osSubMenu->addAction(m_fedora);
m_os->setMenu(m_osSubMenu);
m_appSubMenu->addAction(m_amazon);
m_appSubMenu->addAction(m_github);
m_appSubMenu->addAction(m_facebook);
m_appSubMenu->addSeparator();
m_appSubMenu->addAction(m_photoshop);
m_app->setMenu(m_appSubMenu);
m_mainMenu->addAction(m_details);
m_mainMenu->addSeparator();
m_mainMenu->addAction(m_os);
m_mainMenu->addAction(m_app);
m_mainMenu->addAction(m_settings);
ui.serviceType->setMenu(m_mainMenu)
效果圖:
QStyleSheet
上面示例展示出原生態(tài)樣式分俯,灰蒙蒙不好看。那么用QSS對(duì)外形樣式進(jìn)行改造寝志,我們將所有的樣式語(yǔ)句放到一個(gè)*.qss文件中娇斑,然后在main函數(shù)中加載策添。需要注意的是,我們應(yīng)該將.qss文件添加到.qrc文件中進(jìn)行編譯毫缆。每一次修改.qss文件之后應(yīng)該重新編譯.qrc文件唯竹。否則在界面上將看不出任何改變。代碼如下:
QFile file(``":/ThemeRoller/style"``);
file.open(QFile::ReadOnly);
qApp->setStyleSheet(file.readAll());
file.close();
先考慮將QPushButton作為練手對(duì)象悔醋,編寫如下QSS代碼:
QPushButton {
background: white;
border: 1px solid rgb (41, 57, 85);
border-radius: 3px; # 設(shè)置邊框具備3個(gè)像素的圓角
font-weight: bold; # 字體設(shè)置為加粗
}
QPushButton:hover {
background: lightgray;
}
效果對(duì)比如下:
image