每天一个小程序--在python的类和函数中使用静态变量
Lyrics--老男孩片尾曲

每天一个小程序--Qt结合Designer快速进行快发

pingf posted @ Tue, 02 Nov 2010 09:11:18 -1100 in 未分类 , 3209 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>
编程方面明天不再更新了,要好好复习考研,虽然说这不是第一次提醒自己了...
Matthew Wade said:
Wed, 20 Nov 2024 07:55:22 -1100

Hello, i like the way you post on your blog.  【フル視聴】

Matthew Wade said:
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. 【動画ダウンロード】

Matthew Wade said:
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… 【動画ダウンロード】


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter