PDA

View Full Version : qt design and adding other code



cxl2253
1st February 2007, 03:18
i use the QT Designer and create a simple UI file inhertied from QMainwindow,in its Construct function,I add a treewidget and textedit component, but how to layout them?
I Add a QVboxlayout, but it did not work.
Following is my code :


frmMainWindow::frmMainWindow()
{
setupUi(this);
connect(radioButton,SIGNAL(toggled(bool)),this,SLO T(oncheckBoxclick(bool)));

QVBoxLayout *vboxlayout=new QVBoxLayout();

QTreeWidget *treeWidget = new QTreeWidget(this);
//treeWidget->setGeometry(QRect(0,0,200,100));
treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
for (int i = 0; i < 100; ++i)
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(0, items);
treeWidget->setSizeIncrement(10,50);
treeWidget->setMinimumSize(100,500) ;
treeWidget->setLayout(vboxlayout);

QTextEdit *edit=new QTextEdit(this);
edit->insertPlainText("11111111111111111111111111111111111111111111111111");
edit->insertPlainText("22222222222222222222222222222222222222222222222222");
edit->setMinimumSize(100,100);
edit->setLayout(vboxlayout);

vboxlayout->addSpacing(6);
//vboxlayout->addWidget(treeWidget);
//vboxlayout->addWidget(edit);
vboxlayout->addStretch(100) ;

setLayout(vboxlayout);
}

other more, when I put the QTreewidget in QMainwindow directly in the QT Designer ,not create in Constructor using code,then uic the ui file and moc the file ,but when i run ,I can not focus all the widget ,even the form. it just like in dead loop and can not response to any event !


i save the ui to test.ui, then uic to get the test.h .
create cfrmmain.h from test.h, then moc the cfrmmain to get moc_cfrmmain.h,
implement the cfrmmain.h in cfrmmain.cpp.


/////// cfrmmain.h ////////
#include "test.h"
class frmMainWindow:public QMainWindow,public Ui::frmMainwindow
{
Q_OBJECT
public:
frmMainWindow();
//private slots:
// void oncheckBoxclick(bool);

};


/////////cfrmmain.cpp/////
#include "cfrmmain.h"
frmMainWindow::frmMainWindow()
{
setupUi(this);
}


////////main.cpp///////////
#include <QApplication>
#include "cfrmmain.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
frmMainWindow * mainwindow=new frmMainWindow;
mainwindow->show();

return app.exec();
}

cxl2253
1st February 2007, 04:27
Second question i have found a solution . Must give a layout for the mainwindow,now it can get the event.but how to arrange the manual widget ,maybe you can have good advice.

wysota
1st February 2007, 18:12
treeWidget->setLayout(vboxlayout);
edit->setLayout(vboxlayout);
setLayout(vboxlayout);
Didn't you by any chance mean the following?

vboxlayout->addWidget(treeWidget);
vboxlayout->addWidget(edit);
setLayout(vboxlayout);