回复: 【提问】问一个Qt中布局相关的问题
#6 [tasteprogram 09-27 14:02]
你的代码没有问题呀!我的QT版本是3.3.2,你的版本是多少?
在我的3.3.2版本可以正常运行的。
***************main.cpp********************
#include
#include "mywidget.h"
int main( int argc, char** argv )
{
QApplication app( argc, argv );
MyWidget mainwindow;
app.setMainWidget(&mainwindow);
mainwindow.show();
return app.exec();
}
*****************mywidget.cpp**************
#include "mywidget.h"
#include
MyWidget :: MyWidget( QWidget *parent, const char *name)
: QMainWindow( parent, name )
{
QVBoxLayout *layout = new QVBoxLayout( this, 5, 5, "layout" );
pre = new QPushButton("previous", this, "pre");
layout->addWidget(pre);
next = new QPushButton("next", this, "next");
layout->addWidget(next);
resize( 100, 100);
}
****************mywidget.h*******************
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include
#include
#include
class MyWidget : public QMainWindow
{
public:
MyWidget( QWidget *parent = 0, const char *name = 0 );
QPushButton *pre;
QPushButton *next;
};
#endif
*********************************************
还是用designer来设计界面比较好,将自己的代码放在*Impl.cpp中,这样即使以后使用designer改变界面也不会影响到自己写好的代码