0
我正在使用QTabWidget(pHTab在AbstractManagerTab組成)顯示一些數據 (IpTrafficPage和SpecTrafficPage部件), ,我需要動態添加QPushButtons到添加的選項卡。QTabWidget動態更改部件
當我啓動應用程序時,添加的按鈕僅在最後一個選項卡中可見。 什麼可能是錯的?
class AbstractTrafficPage : public QWidget
{
Q_OBJECT
public:
explicit AbstractTrafficPage(QWidget *parent = 0);
void addCommandButton(QWidget *btn);
private:
QVBoxLayout *commBtnsLayout;
};
AbstractTrafficPage::AbstractTrafficPage(QWidget *parent) :
QWidget(parent),
commBtnsLayout(new QVBoxLayout)
{
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(commBtnsLayout);
mainLayout->addStretch();
setLayout(mainLayout);
}
void AbstractTrafficPage::addCommandButton(QWidget *btn)
{
commBtnsLayout->addWidget(btn);
}
class IpTrafficPage : public AbstractTrafficPage
{
Q_OBJECT
public:
explicit IpTrafficPage(QWidget *parent = 0) : AbstractTrafficPage(parent) {};
};
class SpecTrafficPage : public AbstractTrafficPage
{
Q_OBJECT
public:
explicit SpecTrafficPage(QWidget *parent = 0) : AbstractTrafficPage(parent) {};
};
class AbstractManagerTab : public QWidget {
public:
AbstractManagerTab(QWidget *parent);
~AbstractManagerTab();
void addCommandButton(QWidget *btn);
private:
QTabWidget *pHTab;
}
AbstractManagerTab::AbstractManagerTab(QWidget *parent) :
QWidget(parent),
pHTab(new QTabWidget)
{
IpTrafficPage *ipPage = new IpTrafficPage(this);
pHTab->addTab(ipPage, tr("IP);
SpecTrafficPage *specPage = new SpecTrafficPage(this);
pHTab->addTab(specPage, tr("Spec"));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(pHTab);
setLayout(mainLayout);
}
AbstractManagerTab::~AbstractManagerTab()
{
delete pHTab;
}
AbstractManagerTab::addCommandButton(QWidget *btn)
{
for (int index = 0; index < pHTab->count(); ++index) {
AbstractTrafficPage *page = dynamic_cast<AbstractTrafficPage *>
(pHTab->widget(index));
if (page)
page->addCommandButton(btn);
}
}
class StoredRecsTab : public AbstractManagerTab
{
Q_OBJECT
public:
explicit StoredRecsTab(QWidget *parent = 0);
};
StoredRecsTab::StoredRecsTab(QWidget *parent) :
AbstractManagerTab(parent)
{
QPushButton *createRecBtn = new QPushButton(tr("Create"), this);
QPushButton *removeRecBtn = new QPushButton(tr("Remove"), this);
addCommandButton(createRecBtn);
addCommandButton(removeRecBtn);
}
+1對於暗示其他選項,讓我發笑:-P的確,我不會推薦它...... – Boris
非常感謝。我將不得不重新考慮這個架構。 – Pavel