一步步学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();
}



posted @ 2011-08-24 18:16  Podevor  阅读(298)  评论(0编辑  收藏  举报