QDialog下的QTabWidget问题
我在一个QDialog下添加了一个tabWidget,在其下添加了一positionTab,给这个positionTab设置了layout,但是运行后发现positionTab下什么东西都没有。。。
- C/C++ code
QTabWidget *tabWidget = new QTabWidget;QWidget *positionTab = new QWidget();QFormLayout *lightPositionLayout = new QFormLayout();lightPositionLayout->addRow("X:",xpos);lightPositionLayout->addRow("Y:",ypos);lightPositionLayout->addRow("Z:",zpos);positionTab->setLayout(lightPositionLayout);tabWidget->addTab(positionTab,"Position");研究了快3个小时了看不明白到底哪里错了。奇怪的地方在于,下面的代码会让tabWidget的另外一个tab(ambientTab)下显示“This is a low”!
- C/C++ code
QLabel *ambientTab = new QLabel(tr("This is a low"));tabWidget->addTab(ambientTab,tr("Ambient"));有高人解释一下我到底哪里错了吗???还是QDialog下的QTabWidget有问题?
[解决办法]
addRow有问题。
和UI相关的设计,最好使用Qt Designer,不易出错。
下面是我用它生成的代码,你比较一下:
/********************************************************************************
** Form generated from reading UI file 'designerGg6260.ui'
**
** Created: Wed Jul 4 21:55:40 2012
** by: Qt User Interface Compiler version 4.8.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef DESIGNERGG6260_H
#define DESIGNERGG6260_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QFormLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QTabWidget>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
QVBoxLayout *verticalLayout;
QTabWidget *tabWidget;
QWidget *tab;
QWidget *formLayoutWidget;
QFormLayout *formLayout;
QLabel *xLabel;
QLineEdit *xLineEdit;
QLabel *yLabel;
QLineEdit *yLineEdit;
QLabel *zLabel;
QLineEdit *zLineEdit;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(400, 300);
verticalLayout = new QVBoxLayout(Dialog);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
tabWidget = new QTabWidget(Dialog);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tab = new QWidget();
tab->setObjectName(QString::fromUtf8("tab"));
formLayoutWidget = new QWidget(tab);
formLayoutWidget->setObjectName(QString::fromUtf8("formLayoutWidget"));
formLayoutWidget->setGeometry(QRect(70, 30, 160, 80));
formLayout = new QFormLayout(formLayoutWidget);
formLayout->setObjectName(QString::fromUtf8("formLayout"));
formLayout->setContentsMargins(0, 0, 0, 0);
xLabel = new QLabel(formLayoutWidget);
xLabel->setObjectName(QString::fromUtf8("xLabel"));
formLayout->setWidget(0, QFormLayout::LabelRole, xLabel);
xLineEdit = new QLineEdit(formLayoutWidget);
xLineEdit->setObjectName(QString::fromUtf8("xLineEdit"));
formLayout->setWidget(0, QFormLayout::FieldRole, xLineEdit);
yLabel = new QLabel(formLayoutWidget);
yLabel->setObjectName(QString::fromUtf8("yLabel"));
formLayout->setWidget(1, QFormLayout::LabelRole, yLabel);
yLineEdit = new QLineEdit(formLayoutWidget);
yLineEdit->setObjectName(QString::fromUtf8("yLineEdit"));
formLayout->setWidget(1, QFormLayout::FieldRole, yLineEdit);
zLabel = new QLabel(formLayoutWidget);
zLabel->setObjectName(QString::fromUtf8("zLabel"));
formLayout->setWidget(2, QFormLayout::LabelRole, zLabel);
zLineEdit = new QLineEdit(formLayoutWidget);
zLineEdit->setObjectName(QString::fromUtf8("zLineEdit"));
formLayout->setWidget(2, QFormLayout::FieldRole, zLineEdit);
tabWidget->addTab(tab, QString());
verticalLayout->addWidget(tabWidget);
retranslateUi(Dialog);
tabWidget->setCurrentIndex(0);
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
xLabel->setText(QApplication::translate("Dialog", "x", 0, QApplication::UnicodeUTF8));
yLabel->setText(QApplication::translate("Dialog", "y", 0, QApplication::UnicodeUTF8));
zLabel->setText(QApplication::translate("Dialog", "z", 0, QApplication::UnicodeUTF8));
tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("Dialog", "Tab 1", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // DESIGNERGG6260_H