每天一个小程序--Qt结合Designer快速进行快发
pingf
posted @ Tue, 02 Nov 2010 09:11:18 -1100
in 未分类
, 3260 readers
今天写一个小例子,用Qt快发一个简单电话簿
其运行结果如下图
主要需要完成的是一个ListDialog和一个EditDialog
ListDialog的头文件如下
#ifndef LISTDIALOG_H #define LISTDIALOG_H #include <QDialog> #include "EditDialog.h" #include "ui_listdialog.h" class ListDialog : public QDialog { Q_OBJECT public: ListDialog(); private slots: void addItem(); void editItem(); void deleteItem(); private: Ui::ListDialog ui; }; #endif // LISTDIALOG_H
其cpp文件如下
#include "ListDialog.h" ListDialog::ListDialog() : QDialog() { ui.setupUi( this ); connect( ui.addButton, SIGNAL(clicked()), this, SLOT(addItem()) ); connect( ui.editButton, SIGNAL(clicked()), this, SLOT(editItem()) ); connect( ui.deleteButton, SIGNAL(clicked()), this, SLOT(deleteItem()) ); } void ListDialog::addItem() { EditDialog dlg( this ); if ( dlg.exec() == QDialog::Accepted ) ui.list->addItem( dlg.name() + " -- " + dlg.number() ); } void ListDialog::deleteItem() { delete ui.list->currentItem(); } void ListDialog::editItem() { if ( !ui.list->currentItem() ) return; QStringList parts = ui.list->currentItem()->text().split( "--" ); EditDialog dlg( this ); dlg.setName( parts[0].trimmed() ); dlg.setNumber( parts[1].trimmed() ); if ( dlg.exec() == QDialog::Accepted ) ui.list->currentItem()->setText( dlg.name() + " -- " + dlg.number() ); }
EditDialog的头文件如下
#ifndef EDITDIALOG_H #define EDITDIALOG_H #include "ui_editdialog.h" #include <QDialog> class EditDialog : public QDialog { public: EditDialog( QWidget *parent=0 ); const QString name() const; void setName( const QString& ); const QString number() const; void setNumber( const QString& ); private: Ui::EditDialog ui; }; #endif其cpp文件如下
#include "EditDialog.h" EditDialog::EditDialog( QWidget *parent ) : QDialog( parent ) { ui.setupUi( this ); } const QString EditDialog::name() const { return ui.nameEdit->text().replace("--","").trimmed(); } void EditDialog::setName( const QString &name ) { ui.nameEdit->setText( name ); } const QString EditDialog::number() const { return ui.numberEdit->text().replace("--","").trimmed(); } void EditDialog::setNumber( const QString &number ) { ui.numberEdit->setText( number ); }
注意,这里面用到了使用Qt Designer生成的布局文件,在运行qmake时,qt会产生特别的makefile
通过该makefile,我们运行make时,还会调用uic来产生ui_xxx头文件,还会调用moc来产生所需的元对象所需的cpp文件
最后列出用Qt生成的Ui文件,保存时保存为*.ui文件即可用Qt Designer打开编辑了
listdialog.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>ListDialog</class> <widget class="QDialog" name="ListDialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>401</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Phone Book</string> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QListWidget" name="list"/> </item> <item row="0" column="1"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QPushButton" name="addButton"> <property name="text"> <string>Add new</string> </property> </widget> </item> <item> <widget class="QPushButton" name="editButton"> <property name="text"> <string>Edit</string> </property> </widget> </item> <item> <widget class="QPushButton" name="deleteButton"> <property name="text"> <string>Delete</string> </property> </widget> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <widget class="QPushButton" name="clearButton"> <property name="text"> <string>Clear All</string> </property> </widget> </item> </layout> </item> </layout> </widget> <resources/> <connections> <connection> <sender>clearButton</sender> <signal>clicked()</signal> <receiver>list</receiver> <slot>clear()</slot> <hints> <hint type="sourcelabel"> <x>316</x> <y>286</y> </hint> <hint type="destinationlabel"> <x>145</x> <y>194</y> </hint> </hints> </connection> </connections> </ui>
editdialog.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>EditDialog</class> <widget class="QDialog" name="EditDialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>374</width> <height>109</height> </rect> </property> <property name="windowTitle"> <string>Editor</string> </property> <layout class="QGridLayout" name="gridLayout_2"> <item row="0" column="0"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QLabel" name="nameLabel"> <property name="text"> <string>Name:</string> </property> <property name="buddy"> <cstring>nameEdit</cstring> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="nameEdit"/> </item> <item row="1" column="0"> <widget class="QLabel" name="numberLabel"> <property name="text"> <string>Number:</string> </property> <property name="buddy"> <cstring>numberEdit</cstring> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="numberEdit"/> </item> </layout> </item> <item row="1" column="0"> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>5</height> </size> </property> </spacer> </item> <item row="2" column="0"> <widget class="QDialogButtonBox" name="buttonBox"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="standardButtons"> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> </item> </layout> </widget> <tabstops> <tabstop>nameEdit</tabstop> <tabstop>numberEdit</tabstop> <tabstop>buttonBox</tabstop> </tabstops> <resources/> <connections> <connection> <sender>buttonBox</sender> <signal>accepted()</signal> <receiver>EditDialog</receiver> <slot>accept()</slot> <hints> <hint type="sourcelabel"> <x>227</x> <y>91</y> </hint> <hint type="destinationlabel"> <x>157</x> <y>100</y> </hint> </hints> </connection> <connection> <sender>buttonBox</sender> <signal>rejected()</signal> <receiver>EditDialog</receiver> <slot>reject()</slot> <hints> <hint type="sourcelabel"> <x>295</x> <y>91</y> </hint> <hint type="destinationlabel"> <x>286</x> <y>100</y> </hint> </hints> </connection> </connections> </ui>
编程方面明天不再更新了,要好好复习考研,虽然说这不是第一次提醒自己了...
Wed, 20 Nov 2024 07:55:22 -1100
Hello, i like the way you post on your blog. 【フル視聴】
Thu, 21 Nov 2024 08:31:10 -1100
Before you decide to create your own checklist to incorporate an idea associated with what camping checklist ought to. 【動画ダウンロード】
Thu, 21 Nov 2024 08:32:08 -1100
It seems you are getting quite a lof of unwanted comments. Maybe you should look into a solution for that. Hmm… 【動画ダウンロード】
Mon, 25 Nov 2024 00:22:20 -1100
Most beneficial gentleman speeches and toasts are made to enliven supply accolade up to the wedding couple. Newbie audio system the attention of loud crowds should always think about typically the great norm off presentation, which is their private. best man speaches eスポーツ
Mon, 25 Nov 2024 00:22:35 -1100
I want looking at and I believe this website got some really useful stuff on it! . eスポーツ
Mon, 25 Nov 2024 00:23:31 -1100
But wanna comment that you have a very nice internet site , I love the style and design it really stands out. eスポーツ
Mon, 25 Nov 2024 00:23:47 -1100
I am glad to be one of many visitors on this outstanding web site thanks for posting . eスポーツ
Mon, 25 Nov 2024 00:24:03 -1100
Thank you of this blog. That’s all I’m able to say. You definitely have made this web site into an item thats attention opening in addition to important. You definitely know a great deal of about the niche, youve covered a multitude of bases. Great stuff from this the main internet. All over again, thank you for the blog. eスポーツ
Mon, 25 Nov 2024 00:24:43 -1100
I have been reading out some of your stories and i can claim pretty nice stuff. I will surely bookmark your blog. 英会話 個人レッスン
Tue, 26 Nov 2024 05:10:30 -1100
Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. 営業代行
I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before. 巨根動画
Nice post mate, keep up the great work, just shared this with my friendz となりのあやねさん
Have you ever considered publishing an e-book or guest authoring on other sites? I have a blog based upon on the same subjects you discuss and would really like to have you share some stories/information. I know my readers would value your work. If you are even remotely interested, feel free to shoot me an email. 手淫快楽地獄コース施術同意書
I wish more authors of this type of content would take the time you did to research and write so well. I am very impressed with your vision and insight. ウェディングドレス レンタル
Just lately, I didnrrrt offer lots of consideration for you to departing answers on-page page accounts and still have positioned replies actually much less. Examining by using your enjoyable article, can assist myself to do this sometimes. しみけんのセックステクニック快感制覇
Wed, 27 Nov 2024 04:44:55 -1100
Thanks for taking the time to discuss this topic. I really appreciate it. I’ll stick a link of this entry in my blog. マンツーマン英会話
Sat, 30 Nov 2024 20:18:31 -1100
Outstanding post, I conceive people should larn a lot from this weblog its very user friendly . 転生のエロ実写
Sat, 30 Nov 2024 20:18:46 -1100
Some truly nice and useful info on this site, likewise I conceive the style holds excellent features. 英会話 初心者
Sat, 30 Nov 2024 20:19:06 -1100
Very nice publish, i certainly love this web site, carry on it 【フル視聴】
Mon, 02 Dec 2024 01:32:26 -1100
I view something genuinely special in this website. 浅草 天婦羅
Mon, 02 Dec 2024 01:32:47 -1100
This website is my aspiration , very wonderful pattern and perfect articles . エロ漫画・同人まとめサイト
Mon, 02 Dec 2024 01:33:26 -1100
Some really great info , Gladiola I found this. 素人ギャルエロ動画
Tue, 03 Dec 2024 02:44:35 -1100
I am curious to find out what blog platform you are using? I’m having some minor security problems with my latest website and I’d like to find something more risk-free. Do you have any solutions? 英会話講師募集
Tue, 03 Dec 2024 02:44:48 -1100
What a fantastic post you have made. I just stopped in to tell you I really enjoyed the read and shall be dropping by from time to time from now on. 子供英会話
Sun, 08 Dec 2024 04:04:48 -1100
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. ECサイト セキュリティ
Sun, 08 Dec 2024 04:05:08 -1100
I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... EC-CUBE カスタマイズ