首页 > 代码库 > 【qt学习002】各类位置信息和各类标准输入框

【qt学习002】各类位置信息和各类标准输入框

不得不吐(赞)槽(扬)一下《Linux窗口程序设计—Qt4精彩实例分析》,书中的示例基本上是不完整的,只给出了相应知识点的实现,若要完整地运行整个工程,这需要读者来添加其余代码。在添加代码的过程中,因为不熟,常常是战战兢兢的,查很多资料,然后才敢往上写点代码,有时候挺浪费时间的。

今天学示例3和4,各类位置信息,各类标准输入框。

在编译时遇见一个错误:undefined reference to vtable for。

查资料找到解决方法,先qmake然后再构建,再运行即可。虽然解决了这个问题,但我完全不清楚错在哪里,网上的意见也都莫衷一是,只能等经验积累了,再来分析问题。

代码:

?
// geometry.h
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QDialog>
#include <QWidget>
#include <QGridLayout>
#include <QFrame>
#include <QString>
#include <QLabel>
 
 
 
class Geometry: public QDialog
{
    Q_OBJECT
public:
    Geometry(QWidget *parent = 0, Qt::WindowFlags f = 0);
    ~Geometry();
 
    // 声明所需的空间,主要为QLabel类
    QLabel * xLabel;
    QLabel * xString;
    QLabel * yLabel;
    QLabel * yString;
    QLabel * frameGeoLabel;
    QLabel * frameGeoString;
    QLabel * posLabel;
    QLabel * posString;
    QLabel * geoLabel;
    QLabel * geoString;
    QLabel * widthLabel;
    QLabel * widthString;
    QLabel * heightLabel;
    QLabel * heightString;
    QLabel * rectLabel;
    QLabel * rectString;
    QLabel * sizeLabel;
    QLabel * sizeString;
 
    QGridLayout *layout;
 
    void updateLabel();
protected:
    void moveEvent(QMoveEvent*);
    void resizeEvent(QResizeEvent*);
};
 
 
#endif // GEOMETRY_H
 
 
 
//geometry.cpp
 
#include "geometry.h"
 
Geometry::Geometry(QWidget *parent, Qt::WindowFlags f)
    :QDialog(parent, f)
{
    setWindowTitle("Geometry");
 
    // 创建程序所需的各控件
    xString = new QLabel;
    xString->setText(tr("x():"));
    xLabel = new QLabel;
 
    yString = new QLabel;
    yString->setText(tr("y():"));
    yLabel = new QLabel;
 
    frameGeoString = new QLabel;
    frameGeoString->setText(tr("frameGeometry():"));
    frameGeoLabel = new QLabel;
 
    posString = new QLabel;
    posString->setText(tr("pos():"));
    posLabel = new QLabel;
 
    geoString = new QLabel;
    geoString->setText(tr("geometry():"));
    geoLabel = new QLabel;
 
    widthString = new QLabel;
    widthString->setText(tr("width():"));
    widthLabel = new QLabel;
 
    heightString = new QLabel;
    heightString->setText(tr("height()"));
    heightLabel = new QLabel;
 
    rectString = new QLabel;
    rectString->setText(tr("rect():"));
    rectLabel = new QLabel;
 
    sizeString = new QLabel;
    sizeString->setText(tr("size():"));
    sizeLabel = new QLabel;
 
    // 布局
    setWindowTitle(tr("GeoMetrys "));
    layout = new QGridLayout(this);
    layout->addWidget(xString, 0, 0);
    layout->addWidget(xLabel,0, 1);
    layout->addWidget(yString, 1,0);
    layout->addWidget(yLabel, 1, 1);
    layout->addWidget(frameGeoString, 2, 0);
    layout->addWidget(frameGeoLabel, 2, 1);
    layout->addWidget( posString, 3, 0);
    layout->addWidget( posLabel, 3, 1);
    layout->addWidget( geoString, 4, 0);
    layout->addWidget( geoLabel, 4, 1);
    layout->addWidget( widthString, 5, 0);
    layout->addWidget( widthLabel, 5, 1);
    layout->addWidget( heightString, 6, 0);
    layout->addWidget( heightLabel, 6, 1);
    layout->addWidget( rectString, 7, 0);
    layout->addWidget( rectLabel, 7, 1);
    layout->addWidget( sizeString, 8, 0);
    layout->addWidget( sizeLabel, 8, 1);
 
 
    layout->setMargin(15);
    layout->setSpacing(10);
 
 
 
    updateLabel();
}
 
Geometry::~Geometry()
{
    delete xLabel;
    delete xString;
    delete yLabel;
    delete yString;
    delete frameGeoLabel;
    delete frameGeoString;
    delete posLabel;
    delete posString;
    delete geoLabel;
    delete geoString;
    delete widthLabel;
    delete widthString;
    delete heightLabel;
    delete heightString;
    delete rectLabel;
    delete rectString;
    delete sizeLabel;
    delete sizeString;
    delete layout;
 
}
 
void Geometry::updateLabel()
{
    QString temp;
    QString str_x;
    xLabel->setText(str_x.setNum(x()));
 
    QString str_y;
    yLabel->setText(str_y.setNum(y()));
 
    QString frameGeo;
    frameGeo = temp.setNum(frameGeometry().x()) + "," +
            temp.setNum(frameGeometry().y()) + "," +
            temp.setNum(frameGeometry().width()) + "," +
            temp.setNum(frameGeometry().height());
    frameGeoLabel->setText(frameGeo);
 
    QString position;
    position = temp.setNum(pos().x()) + "," +temp.setNum(pos().y());
    posLabel->setText(position);
 
    QString geo;
    geo = temp.setNum(geometry().x()) + "," +
            temp.setNum(geometry().y()) + "," +
            temp.setNum(geometry().width()) + "," +
            temp.setNum(geometry().height());
    geoLabel->setText(geo);
 
    QString w;
    widthLabel->setText(w.setNum(width()));
    QString h;
    heightLabel->setText(h.setNum(height()));
 
    QString r;
    r = temp.setNum(rect().x()) + "," + temp.setNum(rect().y()) + "," +
            temp.setNum(rect().width()) + ","  + temp.setNum(rect().height());
    rectLabel->setText(r);
 
    QString s;
    s = temp.setNum(size().width()) + ","  + temp.setNum(size().height());
    sizeLabel->setText(s);
}
 
 
 
void Geometry::moveEvent(QMoveEvent *) //下面两个函数继承自QWidget,所以毋须再Geometry构造函数中显式调用
{
    updateLabel();
}
 
void Geometry::resizeEvent(QResizeEvent *)
{
    updateLabel();
}
 
 
//main.cpp
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include "standarddialogs.h"
#include "geometry.h"
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // StandardDialogs ct;
    // ct.show();
 
 
     
    // QPushButton b("Hello World!");
    // b.show();
    // QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
 
    Geometry my;
    my.show();
 
    return a.exec();
}

 

 

?
// inputdialog.h
#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H
 
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
#include <QInputDialog>
#include <QLineEdit>
 
 
class InputDlg: public QDialog
{
    Q_OBJECT
public:
    InputDlg();
    ~InputDlg();
 
public:
    QPushButton *nameButton;
    QPushButton *sexButton;
    QPushButton *ageButton;
    QPushButton *statureButton;
 
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    QLabel *nameLabel;
    QLabel *sexLabel;
    QLabel *ageLabel;
    QLabel *statureLabel;
 
    QGridLayout *layout;
private slots:
    void slotName();
    void slotSex();
    void slotAge();
    void slotStature();
 
};
 
 
#endif // INPUTDIALOG_H
 
// inputdialog.cpp
 
#include "inputdialog.h"
 
InputDlg::InputDlg()
{
    setWindowTitle(tr("Input Dialog"));
 
    // 创建各标签对象
    label1 = new QLabel(tr("Name:"));
    label2 = new QLabel(tr("Sex:"));
    label3 = new QLabel(tr("Age:"));
    label4 = new QLabel(tr("High:"));
 
 
    // 创建各显示标签
    nameLabel  =  new QLabel(tr("LiMing"));
    nameLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken); //QFrame::Panel  0x0002  QFrame draws a panel to make the contents appear raised or sunken
 
    sexLabel = new QLabel(tr("Male"));
    sexLabel->setFrameStyle(QFrame::Panel | QFrame::Raised);
 
    ageLabel = new QLabel(tr("25"));
    ageLabel->setFrameStyle(QFrame::Panel | QFrame::Raised);
 
    statureLabel = new QLabel(tr("175.5"));
    statureLabel->setFrameStyle(QFrame::Panel | QFrame::Raised);
 
    // 创建各修改按钮
    nameButton = new QPushButton;
    // nameButton->setIcon(QIcon("btn.png"));
    sexButton = new QPushButton;
    // sexButton->setIcon(QIcon("btn.png"));
    ageButton = new QPushButton;
    // ageButton->setIcon(QIcon("btn.png"));
    statureButton = new QPushButton;
    // statureButton->setIcon(QIcon("btn.png"));
 
    // 布局
    layout = new QGridLayout(this);
    layout->addWidget(label1, 0, 0);
    layout->addWidget(nameLabel, 0, 1);
    layout->addWidget(nameButton, 0, 2);
 
    layout->addWidget(label2, 1, 0);
    layout->addWidget(sexLabel, 1, 1);
    layout->addWidget(sexButton, 1, 2);
 
    layout->addWidget(label3, 2, 0);
    layout->addWidget(ageLabel, 2, 1);
    layout->addWidget(ageButton, 2, 2);
 
    layout->addWidget(label4, 3, 0);
    layout->addWidget(statureLabel, 3, 1);
    layout->addWidget(statureButton, 3, 2);
 
 
    // 连接信号与槽函数
    connect(nameButton, SIGNAL(clicked()), this, SLOT(slotName()));
    connect(sexButton, SIGNAL(clicked()), this, SLOT(slotSex()));
    connect(ageButton, SIGNAL(clicked()), this, SLOT(slotAge()));
    connect(statureButton, SIGNAL(clicked()), this, SLOT(slotStature()));
 
 
}
 
InputDlg::~InputDlg()
{
    delete label1;
    delete label2;
    delete label3;
    delete label4;
 
    delete layout;
 
    delete nameLabel;
    delete ageLabel;
    delete sexLabel;
    delete statureLabel;
 
    delete nameButton;
    delete sexButton;
    delete ageButton;
    delete statureButton;
}
 
void InputDlg::slotName()
{
    bool ok;
    QString name = QInputDialog::getText(this, tr("User Name"),    // this 表示父窗口,是QWidget对象指针, The QWidget class is the base class of all user interface objects.
            tr("Please input new name:"), QLineEdit::Normal, nameLabel->text(), &ok);
 
    if(ok & !name.isEmpty()) // 此处书中有错误,将&写成了&&,查了qt的help文档,没发现有&&这个运算符号
    {
        nameLabel->setText(name);
    }
 
}
 
 
void InputDlg::slotSex()
{
    QStringList list;
    list << tr("male") << tr("female");
    bool ok;
    QString sex = QInputDialog::getItem(this, tr("Sex"),
                                        tr("Please select sex:"), list, 0, false, &ok);
    if(ok)
        sexLabel->setText(sex);
}
 
 
void InputDlg::slotAge()
{
    bool ok;
    int age = QInputDialog::getInteger(this, tr("User Age"),
                                       tr("Please input age:"), ageLabel->text().toInt(), 0, 150, 1, &ok);
    if (ok)
        ageLabel->setText(QString(tr("%1")).arg(age)); // 数字显示成字符串的方法,也说明setText必须接收字符串输入,原型为 void setText(const QString &)
   
?
}
 
void InputDlg::slotStature()
{
    bool ok;
    double d = QInputDialog::getDouble(this,    tr("Stature"),
                                       tr("Please input stature:"), 175.00, 0, 230.00, 1, &ok);
 
    if(ok)
        statureLabel->setText(QString(tr("%1")).arg(d));
 
 
}
 
 
// main.cpp
 
 
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include "standarddialogs.h"
#include "geometry.h"
#include "inputdialog.h"
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // StandardDialogs ct;
    // ct.show();
 
 
     
    // QPushButton b("Hello World!");
    // b.show();
    // QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
 
    // Geometry my;
    // my.show();
 
    InputDlg person;
    person.show();
 
    return a.exec();
}