• 9672阅读
  • 0回复

QT自定制Widget开发流程 [复制链接]

上一主题 下一主题
离线obrire
 

只看楼主 倒序阅读 楼主  发表于: 2006-03-23
平台:Windows XP
    MinGW
    QT-WIN32 4.1.1
K:\QTSDK\oms>gcc -v
Reading specs from K:/MinGW/bin/../lib/gcc/mingw32/3.4.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchroniz
ation --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.2 (mingw-special)

此文不能保证在GCC-4.0以上可以顺利通过,但用3.4编译时基本上接近4.0。
在编译Linux 2.4的内核时,不见得4.0就能通过,4.0的语法检查更严格,且
速度也要快一些,这是可以理解的。此方案不适合于Microsoft的cl和nmake。


在此案例中,最初将analogclock.h放在当前路径下,是不正确的。
因为当最初执行qmake -project时,qmake会自动扫描当前路径
及其下子目录的.c/.cpp./cxx和.h/.hpp文件,这样会在project
文件中生成诸如以下格式的文件内容。
 
  HEADERS += colorswatch.h mainwindow.h oms.h toolbar.h plugins_inc/analogclock.h
  FORMS += oms.ui
  SOURCES += colorswatch.cpp main.cpp mainwindow.cpp toolbar.cpp
  RESOURCES += mainwindow.qrc

而plugins_inc是额外的plugins的声明文件目录,不需要Makefile对其做
更多处理,因此在使用qmake -o Makefile xxx.pro之前,应将其删除。
当然,当前的版本,在存在有project文件的目录中,直接执行qmake就可以
生成Makefile了。

如果不删除,则会出现以下错误,自动生成的Makefile会认为存在有moc
编译过的moc_analogclock.cpp。

specs/win32-g++" -o release\moc_analogclock.o release\moc_analogclock.cpp
release\moc_analogclock.cpp:36: error: definition of static data member 'AnalogC
lock::staticMetaObject' of dllimport'd class.
release\moc_analogclock.cpp:36: warning: 'AnalogClock::staticMetaObject' defined
locally after being referenced with dllimport linkage
mingw32-make[1]: *** [release\moc_analogclock.o] Error 1
mingw32-make[1]: Leaving directory `K:/QTSDK/oms'
mingw32-make: *** [release] Error 2
K:\QTSDK\oms>

以下错误的产生,是因为使用uic -o oms.h oms.ui时,自动生成的oms.h
以为analogclock.h就在当前目录,所以修改为

#include "plugins_inc/analogclock.h"

oms.ui是通过Designer生成的,文件内容是一个近似于XML语法的文本文件。
其实准确地说,QT就是采用的XML语法来解析这些资源。内容如下:
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>Form</class>
<widget class="QWidget" name="Form" >
<property name="geometry" >
  <rect>
  <x>0</x>
  <y>0</y>
  <width>400</width>
  <height>300</height>
  </rect>
</property>
<property name="windowTitle" >
  <string>Form</string>
</property>
<widget class="AnalogClock" name="analogClock" >
  <property name="geometry" >
  <rect>
  <x>80</x>
  <y>40</y>
  <width>100</width>
  <height>100</height>
  </rect>
  </property>
  <property name="toolTip" >
  <string>The current time</string>
  </property>
  <property name="whatsThis" >
  <string>The analog clock widget displays the current time.</string>
  </property>
</widget>
<widget class="AnalogClock" name="analogClock_2" >
  <property name="geometry" >
  <rect>
  <x>230</x>
  <y>160</y>
  <width>100</width>
  <height>100</height>
  </rect>
  </property>
  <property name="toolTip" >
  <string>The current time</string>
  </property>
  <property name="whatsThis" >
  <string>The analog clock widget displays the current time.</string>
  </property>
</widget>
</widget>
<pixmapfunction></pixmapfunction>
<customwidgets>
<customwidget>
  <class>AnalogClock</class>
  <extends></extends>
  <header>analogclock.h</header>
  <container>0</container>
  <pixmap></pixmap>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
====================================================================================
In file included from main.cpp:25:
oms.h:9:25: analogclock.h: No such file or directory
In file included from main.cpp:25:
oms.h:14: error: ISO C++ forbids declaration of `AnalogClock' with no type
oms.h:14: error: expected `;' before '*' token
oms.h:15: error: ISO C++ forbids declaration of `AnalogClock' with no type
oms.h:15: error: expected `;' before '*' token
oms.h: In member function `void Ui_Form::setupUi(QWidget*)':
oms.h:21: error: `analogClock' undeclared (first use this function)
oms.h:21: error: (Each undeclared identifier is reported only once for each func
tion it appears in.)
oms.h:21: error: `AnalogClock' has not been declared
oms.h:24: error: `analogClock_2' undeclared (first use this function)
oms.h:24: error: `AnalogClock' has not been declared
oms.h: In member function `void Ui_Form::retranslateUi(QWidget*)':
oms.h:35: error: `analogClock' undeclared (first use this function)
oms.h:37: error: `analogClock_2' undeclared (first use this function)
mingw32-make[1]: *** [release\main.o] Error 1
mingw32-make[1]: Leaving directory `K:/QTSDK/oms'
mingw32-make: *** [release] Error 2
====================================================================================

4.1.1并不能通过
uic再次生成.cpp文件了,调用采用新的方式,只需要生成ui的.h文件就可以了。
值得一提的是,在生成project文件之前,先生成ui文件的.h文件。

QWidget w;
Ui_Form ui;
ui.setupUi(&w);
Ui_Form对像是ui文件自动生成的。参见oms.h:
#ifndef OMS_H
#define OMS_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QWidget>
#include "analogclock.h" ===> #include "plugins_inc/analogclock.h"

class Ui_Form
{
public:
  AnalogClock *analogClock;
  AnalogClock *analogClock_2;

  void setupUi(QWidget *Form)
  {
  Form->setObjectName(QString::fromUtf8("Form"));
  Form->resize(QSize(400, 300).expandedTo(Form->minimumSizeHint()));
  analogClock = new AnalogClock(Form);
  analogClock->setObjectName(QString::fromUtf8("analogClock"));
  analogClock->setGeometry(QRect(80, 40, 100, 100));
  analogClock_2 = new AnalogClock(Form);
  analogClock_2->setObjectName(QString::fromUtf8("analogClock_2"));
  analogClock_2->setGeometry(QRect(230, 160, 100, 100));
  retranslateUi(Form);

  QMetaObject::connectSlotsByName(Form);
  } // setupUi

  void retranslateUi(QWidget *Form)
  {
  Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
  analogClock->setToolTip(QApplication::translate("Form", "The current time", 0, QApplication::UnicodeUTF8));
  analogClock->setWhatsThis(QApplication::translate("Form", "The analog clock widget displays the current time.", 0, QApplication::UnicodeUTF8));
  analogClock_2->setToolTip(QApplication::translate("Form", "The current time", 0, QApplication::UnicodeUTF8));
  analogClock_2->setWhatsThis(QApplication::translate("Form", "The analog clock widget displays the current time.", 0, QApplication::UnicodeUTF8));
  Q_UNUSED(Form);
  } // retranslateUi

};

namespace Ui {
  class Form: public Ui_Form {};
} // namespace Ui

#endif // OMS_H

现在可以make了吧,不行,现在还不能生成你所想要的可执行image,因为
我们使用的plugins名叫analogclock,在Makefile中还不知道怎么来链接呢。
尽管Trolltech建议在使用诸如sql的plugins时,在project文件中加上:

  QT += sql

就可以了,我们能加上我们的analogclock吗?qmake没那么智能的,还得手动
在Makefile.Release或Makefile.Debug手工加入。
====================================================================================
release\main.o(.text$_ZN7Ui_Form7setupUiEP7QWidget[Ui_Form::setupUi(QWidget*)]+
x14a):main.cpp: undefined reference to `_imp___ZN11AnalogClockC1EP7QWidget'
release\main.o(.text$_ZN7Ui_Form7setupUiEP7QWidget[Ui_Form::setupUi(QWidget*)]+
x237):main.cpp: undefined reference to `_imp___ZN11AnalogClockC1EP7QWidget'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [release\oms.exe] Error 1
mingw32-make[1]: Leaving directory `K:/QTSDK/oms'
mingw32-make: *** [release] Error 2
====================================================================================
上面的错识显示,setupUi及AnalogClock的符号表在qmake的默认配置下找不到。
当然是找不到了,因为libcustomwidgetplugin.a没有链接进来嘛。修改Makefile.Release中的
LIBS     =     -L"K:\Qt\4.1.1\lib" -lmingw32 -lqtmain -lQtGui4 -lQtCore4
              -L"plugins_lib" -lcustomwidgetplugin

好了,现在可以make了,生成oms.exe文件,但执行时,请将customwidgetplugin.dll拷至
oms.exe所在目录,或考至WINDOWS系统目录。

本文中使用的是AnalogClock定制控件,可以在Designer中使用。

如果你觉得这样比较麻烦,也可以直接修改mkspecs\win32-g++/qmake.conf
将你的资源加入,这样qmake时,自动将plugins扩展的库链接进你的应用中。
[ 此贴被XChinux在2006-03-24 23:55重新编辑 ]
快速回复
限100 字节
 
上一个 下一个