一步步学Qt,第一天

今天是Qt的第一天,越到了很多的问题,不过还好都已经解决了。下面记录一下:


qt incomplete type


这个问题,我刚遇到的时候,以后是自己疏忽没有写好code,后来去参考demo程序,发现自己没有错误。不错那个提示却一再的出现。于是,google,百度都试了。

后来看到了一个说明,现在我也不知道是那个了。大概如下:

#include <QDialog>

#include <QTextEdit>

 在头文件中引用了其他的class的话,若写成:

class QDialog;

class QTextEdit;

那么就会出现路上的错误提示。

现在还没知道为什么?


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

之后就是使用designer设计了多个ui文件,怎么把他们连在一起的问题。为了从一个ui中取数据到另一个ui文件按中,我纠结了半天,不过现在解决了。

qt自动生成的Dialog是把ui对应的class的对象作为主窗口或主Dialog的一个成员。那个在对ui的设计应用到这个窗体上时就使用了这个对象的setupUi方法。

那么在另一个地方需要绘制新的窗体是,就使用新的ui对应的class的对象来调用setupUi方法。

如果你在自定义class时是继承自这个ui的class那个就可以直接重新实现setupUi方法。

看code

#ifndef STANDARDDIALOG_H
#define STANDARDDIALOG_H

#include <QDialog>
#include <ui_showAllDialog.h>
#include <QTextEdit>
class QDialog;
class QTextEdit;
#include "showinfo.h"
namespace Ui {
    class StandardDialog;
}

class StandardDialog : public QDialog
{
    Q_OBJECT

public:
    explicit StandardDialog(QWidget *parent = 0);
    ~StandardDialog();

private:
    Ui::StandardDialog *ui1;
    Ui::showAllDialog *ui2;
    QDialog *all;
    showInfo *showallinfo;
private slots:
    void setSex();
    void setName();
    void setAge();
    void setStature();
    void showAll();
};

#endif // STANDARDDIALOG_H


#include <QInputDialog>
#include <QMessageBox>
#include "standarddialog.h"
#include "ui_standarddialog.h"

StandardDialog::StandardDialog(QWidget *parent) :
    QDialog(parent),
    ui1(new Ui::StandardDialog)
{
    ui1->setupUi(this);

    connect(ui1->quitapp,SIGNAL(clicked()),this,SLOT(close()));
    connect(ui1->sexSetButton,SIGNAL(clicked()),this,SLOT(setSex()));
    connect(ui1->nameSetButton,SIGNAL(clicked()),this,SLOT(setName()));
    connect(ui1->ageSetButton,SIGNAL(clicked()),this,SLOT(setAge()));
    connect(ui1->statureSetButton,SIGNAL(clicked()),this,SLOT(setStature()));
    connect(ui1->showAllButton,SIGNAL(clicked()),this,SLOT(showAll()));
}

StandardDialog::~StandardDialog()
{
    delete ui1;
}

void StandardDialog::setSex()
{
    QStringList list;
    list<<tr("male")<<tr("famale");
    bool ok;
    QString sex = QInputDialog::getItem(this,tr("Sex"),tr("please choose sex:"),list,0,false,&ok);
    if(ok){
        ui1->sexlineEdit->setText(sex);
    }
}

void StandardDialog::setName()
{
    bool ok;
    QString name = QInputDialog::getText(
                this,tr("UserName"),tr("please enter name:")
                ,QLineEdit::Normal,ui1->namelineEdit->text(),&ok);
    if(ok && !name.isEmpty()){
        ui1->namelineEdit->setText(name);
    }
}

void StandardDialog::setAge()
{
    bool ok;
    int age = QInputDialog::getInteger(this,tr("User age"),tr("please enter age:"),ui1->agelineEdit->text().toInt(),0,150,1,&ok);
    if(ok){
        ui1->agelineEdit->setText(QString(tr("%1")).arg(age));
    }
}

void StandardDialog::setStature()
{
bool ok;
double d = QInputDialog::getDouble(this,tr("stature"),tr("please enter your stature:"),170.00,0,200.00,1,&ok);
if(ok){
    ui1->staturelineEdit->setText(QString(tr("%1")).arg(d));
}
}

void StandardDialog::showAll()
{
    showallinfo = new showInfo;
    QString str = tr("Name:")+ui1->namelineEdit->text()+tr("\n")+
            tr("Sex:")+ui1->sexlineEdit->text()+tr("\n")+
            tr("Age:")+ui1->agelineEdit->text()+tr("\n")+
            tr("Stature:")+ui1->staturelineEdit->text()+tr("\n");
    //QString str = tr("Name:")+ui1->namelineEdit->text();
    showallinfo->setInfo(str);

    showallinfo->viewTextInfo();
    showallinfo->show();
}




#ifndef SHOWINFO_H
#define SHOWINFO_H

#include <QDialog>
#include <QString>
#include "ui_showAllDialog.h"

class showInfo : public QDialog,Ui::showAllDialog
{
    Q_OBJECT
public:
    explicit showInfo(QWidget *parent = 0);
    QString getInfo();
    void setInfo(const QString &str);
    void viewTextInfo();
private:
    QString info;
signals:

public slots:

};

#endif // SHOWINFO_H
对于在ui之间传值的问题。我使用的是继承那个ui的class,在重新实现setupUi方法,同时初始化connect关系,即就是在构造函数中完成。那么对于从一个ui中取数据,我使用了自定义的成员函数来进行,看代码:
#include "showinfo.h"

showInfo::showInfo(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);
    connect(this->quitButton,SIGNAL(clicked()),this,SLOT(hide()));
}

void showInfo::setInfo(const QString &str)
{
    this->info = str;
}

QString showInfo::getInfo()
{
    return this->info;
}

void showInfo::viewTextInfo()
{
    textBrowser->setText(this->getInfo());
}


调用部分:


void StandardDialog::showAll()
{
    showallinfo = new showInfo;
    QString str = tr("Name:")+ui1->namelineEdit->text()+tr("\n")+
            tr("Sex:")+ui1->sexlineEdit->text()+tr("\n")+
            tr("Age:")+ui1->agelineEdit->text()+tr("\n")+
            tr("Stature:")+ui1->staturelineEdit->text()+tr("\n");
    //QString str = tr("Name:")+ui1->namelineEdit->text();
    showallinfo->setInfo(str);

    showallinfo->viewTextInfo();
    showallinfo->show();
}



为了使更多的Qt者能尽快入门Qt,也为了QtQt Creator的快速普及,我们花费大量精力写出了这一系列教程。虽然教程的知识可能很浅显,虽然教程的语言可能规范,但是它却被数十万网友所认可。我们会将这一系列教程一直写下去,它将涉及Qt的方方面面 一、Qt Creator的安装和hello world程序的编写 二、Qt Creator编写多窗口程序 三、Qt Creator登录对话框 四、Qt Creator添加菜单图标 五、Qt Creator布局管理器的使用 六、Qt Creator实现文本编辑 七、Qt Creator实现文本查找 八、Qt Creator实现状态栏显示 九、Qt Creator中鼠标键盘事件的处理实现自定义鼠标指针 十、Qt Creator中实现定时器和产生随机数 十一、Qt 2D绘图(一)绘制简单图形 十二、Qt 2D绘图(二)渐变填充 十三、Qt 2D绘图(三)绘制文字 十四、Qt 2D绘图(四)绘制路径 十五、Qt 2D绘图(五)显示图片 十六、Qt 2D绘图(六)坐标系统 十七、Qt 2D绘图(七)Qt坐标系统深入 十八、Qt 2D绘图(八)涂鸦板 十九、Qt 2D绘图(九)双缓冲绘图简介 二十、Qt 2D绘图(十)图形视图框架简介 二十一、Qt数据库(一)简介 二十二、Qt数据库(二)添加MySQL数据库驱动插件 二十三、Qt数据库(三)利用QSqlQuery类执行SQL语句(一) 二十四、Qt数据库(四)利用QSqlQuery类执行SQL语句(二) 二十五、Qt数据库(五)QSqlQueryModel 二十六、Qt数据库(六)QSqlTableModel 二十七、Qt数据库(七)QSqlRelationalTableModel 二十八、Qt数据库(八)XML(一) 二十九、Qt数据库(九)XML(二) 三十、Qt数据库(十)XML(三) 三十一、Qt 4.7.0及Qt Creator 2.0 beta版安装全程图解 三十二、第一个Qt Quick程序(QML程序) 三十三、体验QML演示程序 三十四、Qt Quick Designer介绍 三十五、QML组件 三十六、QML项目之Image和BorderImage 三十七、Flipable、Flickable和状态与动画 三十八、QML视图 三十九、QtDeclarative模块 四十、使用Nokia Qt SDK开发Symbian和Maemo终端软件 四十一、Qt网络(一)简介 四十二、Qt网络(二)HTTP编程 四十三、Qt网络(三)FTP(一) 四十四、Qt网络(四)FTP(二) 四十五、Qt网络(五)获取本机网络信息 四十六、Qt网络(六)UDP 四十七、Qt网络(七)TCP(一) 四十八、Qt网络(八)TCP(二)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值