我创建了一个带有一堆 QToolButtons 的 QWidget,我设法成功地初始化它并将其显示为一个单独的窗口,使用
myWidget.setVisible(true)
然而,我的最终目标是将此小部件添加到 QToolBar 以显示在我的 QMainWindow 的Qt::LeftToolBarArea上。我将myWidget添加到 QToolBar 使用
myToolBar.addWidget(myWidget)
QToolBar 已成功添加到我的 QMainWindow 中(我可以看到用于在 QMainWindow 的不同工具栏区域周围移动它的句柄,并且可以移动它)。但是我的 QWidget 不可见。我试过了
myToolBar.addWidget(myWidget).setVisible(true)
按照手册的规定,因为setVisible()除非在 QAction 上调用,否则将不起作用。我尝试将其他预制小部件(例如 QPushButton)添加到我的 QToolBar 中,并且已成功可视化。
我需要对我的小部件执行什么特别操作才能使其在 QToolBar 中可见?
问候,
C
<< 编辑 >>
因此,正如我所说,我使用 qtDesigner 创建了 myWidget,这样我就可以向您展示创建的内容,希望不会太长:
OnlineAssemblerPlayer.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OnlineAssemblerPlayer</class>
<widget class="QWidget" name="OnlineAssemblerPlayer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>211</width>
<height>30</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>211</width>
<height>31</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="toolButton_2">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
OnlineAssemblerPlayer.h
#ifndef ONLINEASSEMBLERPLAYER_H
#define ONLINEASSEMBLERPLAYER_H
#include <QWidget>
namespace Ui {
class OnlineAssemblerPlayer;
}
class OnlineAssemblerPlayer : public QWidget
{
Q_OBJECT
public:
explicit OnlineAssemblerPlayer(QWidget *parent = 0);
~OnlineAssemblerPlayer();
private:
Ui::OnlineAssemblerPlayer *ui;
};
#endif // ONLINEASSEMBLERPLAYER_H
OnlineAssemblerPlayer.cc
#include "OnlineAssemblerPlayer.h"
#include "ui_OnlineAssemblerPlayer.h"
OnlineAssemblerPlayer::OnlineAssemblerPlayer(QWidget *parent) :
QWidget(parent),
ui(new Ui::OnlineAssemblerPlayer)
{
ui->setupUi(this);
}
OnlineAssemblerPlayer::~OnlineAssemblerPlayer()
{
delete ui;
}
以及 Qt 生成的 *ui_OnlineAssemblerPlayer.h*
/********************************************************************************
** Form generated from reading UI file 'OnlineAssemblerPlayer.ui'
**
** Created: Wed Jul 4 16:23:39 2012
** by: Qt User Interface Compiler version 4.8.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_ONLINEASSEMBLERPLAYER_H
#define UI_ONLINEASSEMBLERPLAYER_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QToolButton>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_OnlineAssemblerPlayer
{
public:
QWidget *horizontalLayoutWidget;
QHBoxLayout *horizontalLayout;
QToolButton *toolButton_2;
QToolButton *toolButton;
QComboBox *comboBox;
void setupUi(QWidget *OnlineAssemblerPlayer)
{
if (OnlineAssemblerPlayer->objectName().isEmpty())
OnlineAssemblerPlayer->setObjectName(QString::fromUtf8("OnlineAssemblerPlayer"));
OnlineAssemblerPlayer->resize(211, 30);
horizontalLayoutWidget = new QWidget(OnlineAssemblerPlayer);
horizontalLayoutWidget->setObjectName(QString::fromUtf8("horizontalLayoutWidget"));
horizontalLayoutWidget->setGeometry(QRect(0, 0, 211, 31));
horizontalLayout = new QHBoxLayout(horizontalLayoutWidget);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
toolButton_2 = new QToolButton(horizontalLayoutWidget);
toolButton_2->setObjectName(QString::fromUtf8("toolButton_2"));
horizontalLayout->addWidget(toolButton_2);
toolButton = new QToolButton(horizontalLayoutWidget);
toolButton->setObjectName(QString::fromUtf8("toolButton"));
horizontalLayout->addWidget(toolButton);
comboBox = new QComboBox(horizontalLayoutWidget);
comboBox->setObjectName(QString::fromUtf8("comboBox"));
horizontalLayout->addWidget(comboBox);
retranslateUi(OnlineAssemblerPlayer);
QMetaObject::connectSlotsByName(OnlineAssemblerPlayer);
} // setupUi
void retranslateUi(QWidget *OnlineAssemblerPlayer)
{
OnlineAssemblerPlayer->setWindowTitle(QApplication::translate("OnlineAssemblerPlayer", "Form", 0, QApplication::UnicodeUTF8));
toolButton_2->setText(QApplication::translate("OnlineAssemblerPlayer", "...", 0, QApplication::UnicodeUTF8));
toolButton->setText(QApplication::translate("OnlineAssemblerPlayer", "...", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class OnlineAssemblerPlayer: public Ui_OnlineAssemblerPlayer {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_ONLINEASSEMBLERPLAYER_H
然后我在我的 QMainWindow 的构造函数中初始化它,如下所示:
OnlineAssemblerPlayer *mOnlineAssemblerPlayer = new OnlineAssemblerPlayer;
QToolBar *mToolbarAssemblerPlayer = new QToolBar(tr("AssemblerPlayer"));
mToolbarAssemblerPlayer->addWidget(mOnlineAssemblerPlayer);
mToolbarAssemblerPlayer->setMovable(true);
mToolbarAssemblerPlayer->setAllowedAreas(Qt::AllToolBarAreas);
addToolBar(Qt::LeftToolBarArea, mToolbarAssemblerPlayer);