• 5441阅读
  • 1回复

请教QT designer 的使用问题? [复制链接]

上一主题 下一主题
离线anye529
 

只看楼主 倒序阅读 楼主  发表于: 2006-10-13
请教QT designer 的使用问题?
— 本帖被 XChinux 执行加亮操作(2007-08-12) —
请问在QT designer 中如何向类中添加公有数据类型,例如 QTimer *timer 或者QSocket *socket
等?好像没有办法在在界面上拖动这类控件自动生成,如果在.ui文件夹下的隐藏文件中的类声明文件添加,make以后又会消失,请各位高手赐教。
离线浪漫天使
只看该作者 1楼 发表于: 2006-10-14
顾名思义,designer 只是用来设计界面控件的,对于非界面的元素,只好自己手动添加。至于你提到的问题,qt文档里面讨论了三种方法,我记得只有两种:继承和组合。

在qt3中的desinger是可以添加变量了。其实在mfc中一些非界面的成员也是要自己添加的

下面是qt420 帮助文档里面的原话,希望对你有帮助

For example, here's the uic output for a simple helloworld.ui form (some details were removed for simplicity):
  1. namespace Ui {
  2. class HelloWorld
  3. {
  4. public:
  5.   QVBoxLayout *vboxLayout;
  6.   QPushButton *pushButton;
  7.   void setupUi(QWidget *HelloWorld)
  8.   {
  9.       HelloWorld->setObjectName(QString::fromUtf8("HelloWorld"));
  10.       vboxLayout = new QVBoxLayout(HelloWorld);
  11.       vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
  12.       pushButton = new QPushButton(HelloWorld);
  13.       pushButton->setObjectName(QString::fromUtf8("pushButton"));
  14.       vboxLayout->addWidget(pushButton);
  15.       retranslateUi(HelloWorld);
  16.   }
  17. };
  18. }

In this case, the main container was specified to be a QWidget (or any subclass of QWidget). Had we started with a QMainWindow template in Qt Designer, setupUi()'s parameter would be of type QMainWindow.
There are two ways to create an instance of our form. One approach is to create an instance of the Ui::HelloWorld class, an instance of the main container (a plain QWidget), and call setupUi():
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include "ui_helloworld.h"   // defines Ui::HelloWorld
  4. int main(int argc, char *argv[])
  5. {
  6.   QApplication app(argc, argv);
  7.   QWidget w;
  8.   Ui::HelloWorld ui;
  9.   ui.setupUi(&w);
  10.   w.show();
  11.   return app.exec();
  12. }


The second approach is to inherit from both the Ui::HelloWorld class and the main container, and to call setupUi() in the constructor of the subclass. In that case, QWidget (or one of its subclasses, e.g. QDialog) must appear first in the base class list so that moc picks it up correctly. For example:
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include "ui_helloworld.h"   // defines Ui::HelloWorld
  4. class HelloWorldWidget : public QWidget, public Ui::HelloWorld
  5. {
  6.   Q_OBJECT
  7. public:
  8.   HelloWorldWidget(QWidget *parent = 0)
  9.       : QWidget(parent)
  10.   { setupUi(this); }
  11. };
  12. int main(int argc, char *argv[])
  13. {
  14.   QApplication app(argc, argv);
  15.   HelloWorldWidget w;
  16.   w.show();
  17.   return app.exec();
  18. }

This second method is useful when porting Qt 3 forms to Qt 4. HelloWorldWidget is a class whose instance is the actual form and which contains public pointers to all the widgets in it. It therefore has an interface identical to that of a class generated by uic in Qt 3.
快速回复
限100 字节
 
上一个 下一个