千张笔记

Email:rain_qian830@163.com
posts - 28, comments - 42, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

QT笔记(2)-Qt Designer的使用

Posted on 2010-07-20 20:37 千张 阅读(7186) 评论(1)  编辑 收藏 引用 所属分类: QT学习
这几天在看《C++ GUI Qt 4编程》第二版,看到第二章,用到Qt Designer部分,有点迷糊,书的版本可能有点老,命令方式的编译运行弄得我头大,本想先不看这部分,可是第三章要用到这里的两个对话框,不得不硬着头皮看下去。

最后先一步步按着书上的步骤实现,最后想想,怎么没有用到Qt Creator呢,再自己摸索了下,终于知道怎么在Qt Creator中怎么实现了,现在看起来很简单,却让我这个新手捣鼓了半天。

就拿第二章中的gotocell作为例子吧。

第一步:
打开Qt Creator -> File -> New file or Project -> Empty Qt4 Project,工程名(Name)设为gotocell,点确定(Finish)后,会生成一个空工程文件gotocell.pro。
第二步:
在左视图project区域中,可以看到gotocell工程,在gotocell上点右键,选择Add New...,新建一个Qt Designer Form and Class(或者Qt Designer Form,gotocelldialog.h和gotocelldialog.cpp可以自己添加),名字名为gotocelldialog.ui,会相应地生成Form类文件gotocelldialog.h和gotocelldialog.cpp。gotocelldialog.ui的设计就按书上的步骤来做,gotocelldialog.h和gotocelldialog.cpp中的内容如下(与书上一样):
gotocelldialog.h
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include 
<QDialog>

#include 
"ui_gotocelldialog.h"

class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
    Q_OBJECT
public:
    GoToCellDialog(QWidget 
*parent = 0);

private slots:
    
void on_lineEdit_textChanged();
};
#endif // GOTOCELLDIALOG_H
gotocelldialog.cpp
#include <QtGui>

#include 
"gotocelldialog.h"

GoToCellDialog::GoToCellDialog(QWidget 
*parent) : QDialog(parent)
{
    setupUi(
this);

    QRegExp regExp(
"[A-Za-z][1-9][0-9]{0,2}");
    lineEdit
->setValidator(new QRegExpValidator(regExp, this));

    connect(okButton,SIGNAL(clicked()), 
this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), 
this, SLOT(reject()));
}

void GoToCellDialog::on_lineEdit_textChanged()
{
    okButton
->setEnabled(lineEdit->hasAcceptableInput());
}
然后再Add New ->C++ Source File,添加main.cpp文件,main.cpp文件内容如下:
#include <QApplication>
#include 
<QDialog>

#include 
"gotocelldialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);

    GoToCellDialog 
*dialog = new GoToCellDialog;
    dialog
->show();

    
return app.exec();
}
然后Build -> Run,就可以运行了。
ui_gotocelldialog.h文件在编译后自动生成,该文件与gotocelldialog.ui相对应,定义了ui上的窗口部件,以及对窗口部件初始化,具体内容如下:
/********************************************************************************
** Form generated from reading UI file 'gotocelldialog.ui'
**
** Created: Tue Jul 20 12:00:07 2010
**      by: Qt User Interface Compiler version 4.6.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
*******************************************************************************
*/

#ifndef UI_GOTOCELLDIALOG_H
#define UI_GOTOCELLDIALOG_H

#include 
<QtCore/QVariant>
#include 
<QtGui/QAction>
#include 
<QtGui/QApplication>
#include 
<QtGui/QButtonGroup>
#include 
<QtGui/QHBoxLayout>
#include 
<QtGui/QHeaderView>
#include 
<QtGui/QLabel>
#include 
<QtGui/QLineEdit>
#include 
<QtGui/QPushButton>
#include 
<QtGui/QSpacerItem>
#include 
<QtGui/QVBoxLayout>
#include 
<QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_GoToCellDialog
{
public:
    QVBoxLayout 
*verticalLayout;
    QHBoxLayout 
*horizontalLayout;
    QLabel 
*label;
    QLineEdit 
*lineEdit;
    QHBoxLayout 
*horizontalLayout_2;
    QSpacerItem 
*horizontalSpacer;
    QPushButton 
*okButton;
    QPushButton 
*cancelButton;

    
void setupUi(QWidget *GoToCellDialog)
    {
        
if (GoToCellDialog->objectName().isEmpty())
            GoToCellDialog
->setObjectName(QString::fromUtf8("GoToCellDialog"));
        GoToCellDialog
->resize(26082);
        verticalLayout 
= new QVBoxLayout(GoToCellDialog);
        verticalLayout
->setObjectName(QString::fromUtf8("verticalLayout"));
        horizontalLayout 
= new QHBoxLayout();
        horizontalLayout
->setObjectName(QString::fromUtf8("horizontalLayout"));
        label 
= new QLabel(GoToCellDialog);
        label
->setObjectName(QString::fromUtf8("label"));

        horizontalLayout
->addWidget(label);

        lineEdit 
= new QLineEdit(GoToCellDialog);
        lineEdit
->setObjectName(QString::fromUtf8("lineEdit"));

        horizontalLayout
->addWidget(lineEdit);


        verticalLayout
->addLayout(horizontalLayout);

        horizontalLayout_2 
= new QHBoxLayout();
        horizontalLayout_2
->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        horizontalSpacer 
= new QSpacerItem(4020, QSizePolicy::Expanding, QSizePolicy::Minimum);

        horizontalLayout_2
->addItem(horizontalSpacer);

        okButton 
= new QPushButton(GoToCellDialog);
        okButton
->setObjectName(QString::fromUtf8("okButton"));
        okButton
->setEnabled(false);
        okButton
->setDefault(true);

        horizontalLayout_2
->addWidget(okButton);

        cancelButton 
= new QPushButton(GoToCellDialog);
        cancelButton
->setObjectName(QString::fromUtf8("cancelButton"));

        horizontalLayout_2
->addWidget(cancelButton);


        verticalLayout
->addLayout(horizontalLayout_2);

#ifndef QT_NO_SHORTCUT
        label
->setBuddy(lineEdit);
#endif // QT_NO_SHORTCUT

        retranslateUi(GoToCellDialog);

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

    
void retranslateUi(QWidget *GoToCellDialog)
    {
        GoToCellDialog
->setWindowTitle(QApplication::translate("GoToCellDialog""Go to Cell"0, QApplication::UnicodeUTF8));
        label
->setText(QApplication::translate("GoToCellDialog""&Cell Location:"0, QApplication::UnicodeUTF8));
        okButton
->setText(QApplication::translate("GoToCellDialog""OK"0, QApplication::UnicodeUTF8));
        cancelButton
->setText(QApplication::translate("GoToCellDialog""Cancel"0, QApplication::UnicodeUTF8));
    } 
// retranslateUi

};

namespace Ui {
    
class GoToCellDialog: public Ui_GoToCellDialog {};
// namespace Ui

QT_END_NAMESPACE

#endif // UI_GOTOCELLDIALOG_H
其中
namespace Ui
{
    class GoToCellDialog: public Ui_GoToCellDialog {};
}
为命名空间,在Ui空间中生成一个类GoToCellDialog,继承自Ui_GoToCellDialog,可以看到这个类名GoToCellDialog与我们自己在gotocelldialog.h中定义的类名相同,在这里定义命名空间的作用就是为了区分Ui类和我们自己定义的类。

书中还有一句很重要的话:由于多重继承关系,可以直接访问Ui::GoToCellDialog中的成员。创建了用户接口后,setupUi()函数还会自动将那些符合on_objectName_signalName()命名惯例的任意槽与相应的objectName的signalName()信号连接在一起。在这个例子中,就意味着setupUi()函数将建立如下所示的信号-槽连接关系:
connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(on_lineEdit_textChanged()));

最后附上今天学到的几个命令:
1> 在Linux命令提示中,用cd命令来改变当前目录。这是cd命令的一些基本用法:
     1. 改变你的根路径,键入cd,按回车键。
     2. 进入一个子目录,键入cd,空格,然后是子路径名(例如:cd Documents),再按回车键。
     3. 进入当前目录的上一级目录,键入cd,空格,两个点,然后按回车键。
     4. 进入一个特定的目录,键入cd,空格,路径名(例如 cd /usr/local/lib),再按回车键。
     5. 为了确定你所在的目录,你可以键入pwd,按回车键,你将看到你所在的当前目录名称。
2> Qt相关命令
     用cd命令进入到当前目录后:
     qmake -project 命令可生成一个与平台无关的项目文件,此处为gotocell.pro
     qmake gotocell.pro 命令可生成一个与平台相关的makefile文件
     make 命令可构建(Build)该程序,生成可执行文件
     make clean 命令清理掉编译产生的内容,因为make命令执行前,要先检查是否有编译后的文件存在,若存在,则不编译。
     ./gotocell 命令可运行程序



Feedback

# re: QT笔记(2)-Qt Designer的使用  回复  更多评论   

2012-03-02 17:15 by 张永兴
你好,我是刚学习Qt的,是在fedora下安装的,我可以问问你一些问题吗?

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理