axylp123的个人主页

http://www.qtcn.org/bbs/u/154703  [收藏] [复制]

axylp123

  • 1

    关注

  • 1

    粉丝

  • 16

    访客

  • 等级:新手上路
  • 总积分:15
  • 男,1987-10-10

最后登录:2017-06-01

更多资料

日志

2.3 快速设计对话框

2015-11-27 09:59
//UIGoToCellDialog.h
#ifndef UIGOTOCELLDIALOG_H
#define UIGOTOCELLDIALOG_H
#include <QDialog>
#include "ui_UIGoToCellDialog.h"
//多继承
class UIGoToCellDialog : public QDialog,
                                               public Ui::UIGoToCellDialog
{
    Q_OBJECT
public:
    explicit UIGoToCellDialog(QWidget *parent = 0);
signals:
public slots:
    void slotInputChanged();
};
#endif // UIGOTOCELLDIALOG_H
//UIGoToCellDialog.cpp
#include <QtGui>
#include "UIGoToCellDialog.h"
UIGoToCellDialog::UIGoToCellDialog(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);
    setWindowTitle(tr("GoToCellDialog"));
    m_pOKBtn->setEnabled(false);
    //正则表达式
    QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
    m_pInputEdit->setValidator(new QRegExpValidator(regExp, this));
    //accept()和reject()都可以关闭对话框
    //可以通过判断 QDialog::exec() 的返回值获取到用户按下的按钮
    //accept() 返回 QDialog::Accepted 值为 1
    //reject() 返回 QDialog::Rejected 值为 0
    connect(m_pOKBtn, SIGNAL(clicked()), this, SLOT(accept()));
    connect(m_pCancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
    connect(m_pInputEdit, SIGNAL(textChanged(QString)),
            this, SLOT(slotInputChanged()));
}
void UIGoToCellDialog::slotInputChanged()
{
    //hasAcceptableInput() 使用构造函数中创建的检验器对输入值进行检测
    m_pOKBtn->setEnabled(m_pInputEdit->hasAcceptableInput());
}
#include <QApplication>
#include "UIGoToCellDialog.h"
#include <QDebug>
int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    UIGoToCellDialog *pDialog = new UIGoToCellDialog;
    int rel = pDialog->exec();
    if (rel == QDialog::Accepted)
    {
        qDebug()<<"accept";
    }
    else if (rel == QDialog::Rejected)
    {
        qDebug()<<"rejected";
    }
    app.quit();
    return app.exec();
}
//UIGoToCellDialog.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UIGoToCellDialog</class>
<widget class="QDialog" name="UIGoToCellDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>294</width>
    <height>71</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>&Cell Location:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="m_pInputEdit"/>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_2">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="m_pOKBtn">
       <property name="text">
        <string>OK</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="m_pCancelBtn">
       <property name="text">
        <string>Cancel</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
</widget>
<resources/>
<connections/>
</ui>

以上效果也可以使用QDialogButtonBox实现
分类:C++GUIQt4|回复:0|浏览:763|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:04-29 03:03
©2005-2016 QTCN开发网 版权所有 Gzip disabled