我创建了一个QWidget,其中包含一堆QToolButtons,我成功地初始化了它,并将它显示为一个单独的窗口。
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
<<编辑>>
因此,正如我所说的,我使用myWidget创建了qtDesigner,这样我就可以向您展示所创建的内容,希望不会太长:
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);
发布于 2012-07-04 15:50:11
您不必设置工具栏的可见性。您确定在小部件的外观中添加了什么吗?我几乎可以肯定,您没有设置任何小部件外观。您能给我们展示一下“myWidget”的代码吗?从添加小部件到工具栏,还能显示更多内容吗?
编辑
试着添加
setLayout(ui->horizontalLayout);
就在
retranslateUi(OnlineAssemblerPlayer);
发布于 2017-09-04 19:31:39
我无意中遇到了类似的问题:创建了一个自定义小部件(带有成员QWidget
的QListView
的QListView
子类),并试图将其添加到QListView
的QToolBar
中。不管我做了什么,它都没有出现,尽管成功地修复了QToolBar
。尽管简单的默认小部件(按钮、文本窗口,甚至列表视图)都是可见的,而且运行良好。
最后,我没有让它工作,但是我发现QToolBar
实际上不是使用QWidgets
,而是使用QActions
:甚至通过addWidget( wgt *)添加小部件,它只添加了一些与wgt连接的QAction。
现在,添加由一些默认小部件(如QButton )组成的自定义小部件似乎是合乎逻辑的,QToolBar只是不知道如何显示它。据我所知,它是用QWidgetAction
解决的,它是为了向QToolBar
、QMenu
等添加自定义小部件功能而实现的--所有这些都使用QActions
。QWidgetAction
的虚拟函数createWidget(QWidget*)
和deleteWidget(QWidget*)
就是为了实现这一目标。
但在我的例子中,我避免了这个问题:不是从QWidget
继承的,而是从QListView
中添加QListView
。通过这种方式,我可以自由地将它添加到工具栏中,并使用它的addWidget()
。
https://stackoverflow.com/questions/11332119
复制相似问题