【发布时间】:2011-01-31 23:09:53
【问题描述】:
我正在 Qt 中创建一个应用程序,它允许用户在 QGraphicsView 中拖动各种“模块”。 Whenever one of these modules is selected, it emits a signal which is then picked up by a "ConfigurationWidget" which is a side-panel which should display all of the relevant parameters of the selected module:
class ConfigurationWidget : public QWidget
{
Q_OBJECT
public:
ConfigurationWidget(QWidget *parent) : QWidget(parent) {}
public slots:
void moduleSelected(Module* m)
{
if(layout())
{
while (itsLayout->count()>0)
{
delete itsLayout->takeAt(0);
}
}
delete layout();
itsLayout = new QFormLayout(this);
itsLayout->addRow(QString(tr("Type:")), new QLabel(m->name()));
itsLayout->addRow(QString(tr("Instance:")), new QLabel(m->instanceID()));
// ... Display a whole bunch of other fields that depends on the module
}
};
The problem is that the ConfigurationWidget never actually gets cleared when a module is selected.新字段只是在旧字段上绘制。我尝试了 hide() 和 show()、invalidate()、update() 等的各种组合,但均无济于事。
制作一个可以像这样动态更改其字段的小部件的正确方法是什么?
【问题讨论】:
-
你应该使用 QStackedWidget