前言
本文主要将记录以下五部分:
- 父窗口
- 信号和槽
- 面向对象Qt编程
- Qt设计师
- Qt创造器
00 QT 环境搭建
QT源码和SDK下载
01 父窗口
- QWidget
- QMainWindow(主窗口)//QWidget的直接子类
- QDialog(对话框)//QWidget的直接子类
 
02 信号和槽(实现QSlider与QSpinBox)

1.1 初始代码
#include <QApplication>
#include <QSlider>
#include <QSpinBox>
#include <QMainWindow>
#include <QObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow myMainWindow;
myMainWindow.setWindowTitle("滑块 选值框");
myMainWindow.resize(340,240);
QSlider myslider(Qt::Horizontal,&myMainWindow);
myslider.move(20,100);
myslider.setRange(0,200);
QSpinBox myspinbox(&myMainWindow);
myspinbox.move(220,100);
myspinbox.setRange(0,200);
myMainWindow.show();
QObject::connect(&myslider,&QSlider::valueChanged,&myspinbox,&QSpinBox::setValue);
QObject::connect(&myspinbox,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),&myslider,&QSlider::setValue);
return a.exec();
}
1.2 运行结果

1.3 改进思想
- 此处应该使用
水平布局器<QHBoxLayout> 来优化,还有使用面向对象思维 来完成封装!
03 面向对象的QT编程
3.1 加法计算器

3.1.1 calculatordialog.h
#ifndef __CALCULATORDIALOG_H
#define __CALCULATORDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QDoubleValidator>
class CalculatorDialog:public QDialog
{
Q_OBJECT
public:
CalculatorDialog(void);
public slots:
void enableButton(void);
void calcClicked(void);
private:
QLineEdit* m_editX;
QLineEdit* m_editY;
QLineEdit* m_editZ;
QLabel* m_label;
QPushButton* m_button;
};
#endif
3.1.2 calculatordialog.cpp
#include "calculatordialog.h"
CalculatorDialog::CalculatorDialog(void)
{
setWindowTitle("计算器");
m_editX = new QLineEdit(this);
m_editX->setAlignment(Qt::AlignRight);
m_editX->setValidator(new QDoubleValidator(this));
m_editY = new QLineEdit(this);
m_editY->setAlignment(Qt::AlignRight);
m_editY->setValidator(new QDoubleValidator(this));
m_editZ = new QLineEdit(this);
m_editZ->setAlignment(Qt::AlignRight);
m_editZ->setReadOnly(true);
m_label = new QLabel("+",this);
m_button = new QPushButton("=",this);
m_button->setEnabled(false);
QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(m_editX);
layout->addWidget(m_label);
layout->addWidget(m_editY);
layout->addWidget(m_button);
layout->addWidget(m_editZ);
setLayout(layout);
connect(m_editX,SIGNAL(textChanged(QString)),
this,SLOT(enableButton(void)));
connect(m_editY,SIGNAL(textChanged(QString)),
this,SLOT(enableButton(void)));
connect(m_button,SIGNAL(clicked(void)),
this,SLOT(calcClicked(void)));
}
void CalculatorDialog::enableButton(void){
bool bXOK, bYOK;
m_editX->text().toDouble(&bXOK);
m_editY->text().toDouble(&bYOK);
m_button->setEnabled(bXOK && bYOK);
}
void CalculatorDialog::calcClicked(void){
double res = m_editX->text().toDouble() +
m_editY->text().toDouble();
QString str = QString::number(res);
m_editZ->setText(str);
}
3.1.3 main.cpp
#include "calculatordialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CalculatorDialog calc;
calc.show();
return app.exec();
}
2.1.4 运行结果

3.2 获取系统时间

3.2.1 timedialog.h
#ifndef __TIMEDIALOG_H
#define __TIMEDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTime>
#include <QDebug>
class TimeDialog : public QDialog{
Q_OBJECT
public:
TimeDialog(void);
signals:
void mySignal(const QString&);
public slots:
void getTime(void);
private:
QLabel* m_label;
QPushButton* m_button;
};
#endif
3.2.2 timedialog.cpp
#include "timedialog.h"
TimeDialog::TimeDialog(void)
{
m_label = new QLabel(this);
m_label->setFrameStyle(QFrame::Panel|QFrame::Sunken);
m_label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
QFont font;
font.setPointSize(20);
m_label->setFont(font);
m_button = new QPushButton("获取当前时间");
m_button->setFont(font);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(m_label);
layout->addWidget(m_button);
setLayout(layout);
connect(m_button,SIGNAL(clicked(void)),
this,SLOT(getTime(void)));
connect(this,SIGNAL(mySignal(QString)),
m_label,SLOT(setText(QString)));
}
void TimeDialog::getTime(void){
qDebug("getTime");
qDebug() << "getTime";
QTime time = QTime::currentTime();
QString str = time.toString("hh:mm:ss");
mySignal(str);
}
3.2.3 main.cpp
#include "timedialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TimeDialog w;
w.show();
return a.exec();
}
3.2.4 运行结果

04. QT设计师

UIC

4.1 使用designer设计登录对话框

4.1.1 logindialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginDialog</class>
<widget class="QDialog" name="LoginDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>593</width>
<height>324</height>
</rect>
</property>
<property name="font">
<font>
<family>微软雅黑</family>
<pointsize>20</pointsize>
</font>
</property>
<property name="windowTitle">
<string>登录</string>
</property>
<property name="sizeGripEnabled">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>54</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>用户名:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="m_usernameEdit"/>
</item>
<item row="1" column="0" rowspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>密 码:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="m_passwordEdit">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<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="QDialogButtonBox" name="m_btnBox">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>53</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
4.1.2 logindialog.h
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class LoginDialog; }
QT_END_NAMESPACE
class LoginDialog : public QDialog
{
Q_OBJECT
public:
LoginDialog(void);
~LoginDialog();
public slots:
void onAccepted(void);
void onRejected(void);
private:
Ui::LoginDialog *ui;
};
#endif
4.1.3 logindialog.cpp
#include "logindialog.h"
#include "ui_logindialog.h"
LoginDialog::LoginDialog(void): ui(new Ui::LoginDialog)
{
ui->setupUi(this);
connect(ui->m_btnBox,SIGNAL(accepted(void)),
this,SLOT(onAccepted(void)));
connect(ui->m_btnBox,SIGNAL(rejected(void)),
this,SLOT(onRejected(void)));
}
void LoginDialog::onAccepted(void){
if(ui->m_usernameEdit->text() == "terna" &&
ui->m_passwordEdit->text() == "123456"){
qDebug() << "登录成功";
close();
}else{
QMessageBox msgBox(
QMessageBox::Critical,
"Error",
"用户名或密码错误",
QMessageBox::Ok,
this);
msgBox.exec();
}
}
void LoginDialog::onRejected(void){
QMessageBox msgBox(
QMessageBox::Question,
"登录",
"是否确定要取消登录?",
QMessageBox::Yes|QMessageBox::No,
this);
if(msgBox.exec() == QMessageBox::Yes){
close();
}
}
LoginDialog::~LoginDialog()
{
delete ui;
}
4.1.4 main.cpp
#include "logindialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LoginDialog w;
w.show();
return a.exec();
}
4.1.5 运行结果

05.QT创造器

后记
进阶笔记到此结束
|