我在Qt写过QMenu和QToolBar。 这就是我得到的:
我无法找到任何方法在QMenu(文件,编辑)和QToolBar(带钢琴图标的两个按钮)之间删除此白色分隔符。
代码:
MainWindow::MainWindow()
{
this->setStyleSheet("background-color: black;");
initMenu();
initButtons();
}
void MainWindow::initMenu()
{
menuBar()->setStyleSheet("background: #555555; "
"color: #EEEEEE; "
"selection-background-color: #222222; "
"border-color:#EEEEEE;");
QMenu *fileMenu = menuBar()->addMenu("File");
QAction *newFileAction = new QAction("New", this);
newFileAction->setShortcut(QKeySequence::New);
newFileAction->setStatusTip("Create new file.");
//connect
fileMenu->addAction(newFileAction);
QAction *openFileAction = new QAction("Open", this);
openFileAction->setShortcut(QKeySequence::Open);
openFileAction->setStatusTip("Open file.");
//connect
fileMenu->addAction(openFileAction);
QAction *saveFileAction = new QAction("Save", this);
saveFileAction->setShortcut(QKeySequence::Save);
saveFileAction->setStatusTip("Save file.");
//connect
fileMenu->addAction(saveFileAction);
QAction *saveAsFileAction = new QAction("Save as", this);
saveAsFileAction->setShortcut(QKeySequence::SaveAs);
saveAsFileAction->setStatusTip("Save file as another file.");
//connect
fileMenu->addAction(saveAsFileAction);
fileMenu->addSeparator();
QAction *quitAction = new QAction("Quit", this);
quitAction->setShortcut(QKeySequence::Quit);
quitAction->setStatusTip("Quit program.");
//connect
fileMenu->addAction(quitAction);
QMenu *editMenu = menuBar()->addMenu("Edit");
QAction *copyAction = new QAction("Copy", this);
copyAction->setShortcut(QKeySequence::Copy);
copyAction->setStatusTip("Copy.");
//connect
editMenu->addAction(copyAction);
}
void MainWindow::initButtons()
{
QToolBar *buttonToolBar = addToolBar("Buttons");
buttonToolBar->setStyleSheet("background: #555555");
buttonToolBar->setMovable(false);
const QIcon pianoIcon = QIcon::fromTheme("piano-roll",QIcon(":/buttons/b_piano.png"));
QAction *pianoAction = new QAction(pianoIcon, tr("&Piano Roll"), this);
pianoAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1));
pianoAction->setStatusTip("Opens Piano Roll window.");
//connect();
buttonToolBar->addAction(pianoAction);
const QIcon piano2Icon = QIcon::fromTheme("piano-roll2",QIcon(":/buttons/i_piano.png"));
QAction *piano2Action = new QAction(piano2Icon, tr("&Piano Roll"), this);
piano2Action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_2));
piano2Action->setStatusTip("Opens Piano Roll window number two.");
//connect();
buttonToolBar->addAction(piano2Action);
}
在MainWindow中我运行了两个函数。第一个功能创建了Qmenu。第二个用两个按钮创建QToolBar。