Qt精彩实例2

实例.helloworld
hello.cpp
#include <QApplication>
#include <QPushButton>
 
int main(int argc,char *argv[])
{
QApplication app(argc, argv);
QPushButton b("hello world");
b.show();
QObject::connect(&b, SIGNAL(clicked()), &app, SLOT(quit()));
return app.exec();
}
 
 
实例.文件,颜色,字体对话框
dialog.h
 
#ifndef DIALOG_H
#define DIALOG_H
 
#include <QDialog>
#include <QApplication>
#include <QGridLayout>
#include <QPushButton>
#include <QFrame>
#include <QDialog>
#include <QLineEdit>
 
namespace Ui {
class Dialog;
}
 
class Dialog : public QDialog
{
Q_OBJECT
 
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QGridLayout *layout;
 
QPushButton *filePushButton;
QPushButton *colorPushButton;
QPushButton *fontPushButton;
 
QLineEdit *fileLineEdit;
QLineEdit *fontLineEdit;
QFrame *colorFrame;
 
private:
Ui::Dialog *ui;
private slots:
void slotOpenFileDlg();
void slotOpenColorDlg();
void slotOpenFontDlg();
};
 
#endif // DIALOG_H

dialog.cpp
 
 
 
 
 
#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
#include <QFileDialog>
#include <QColor>
#include <QColorDialog>
#include <QFont>
#include <QFontDialog>
 
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);setWindowTitle(tr("lalala"));
 
layout = new QGridLayout(this);
 
filePushButton = new QPushButton;
filePushButton->setText(tr("file"));
 
colorPushButton = new QPushButton;
colorPushButton->setText(tr("color"));
 
fontPushButton = new QPushButton;
fontPushButton->setText(tr("font"));
 
fileLineEdit = new QLineEdit;
 
colorFrame = new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
 
fontLineEdit = new QLineEdit;
fontLineEdit->setText(tr("hello world"));
 
layout->addWidget(filePushButton,0,0);
layout->addWidget(fileLineEdit,0,1);
 
layout->addWidget(colorPushButton,1,0);
layout->addWidget(colorFrame,1,1);
 
layout->addWidget(fontPushButton,2,0);
layout->addWidget(fontLineEdit,2,1);
 
layout->setMargin(15);
layout->setSpacing(10);
 
connect(filePushButton, SIGNAL(clicked()), this, SLOT(slotOpenFileDlg()));
connect(colorPushButton, SIGNAL(clicked()), this, SLOT(slotOpenColorDlg()));
connect(fontPushButton, SIGNAL(clicked()), this, SLOT(slotOpenFontDlg()));
}
 
Dialog::~Dialog()
{
delete ui;
}
 
void Dialog::slotOpenFileDlg()
{
QString s = QFileDialog::getOpenFileName(
this,
"open file dialog",
"/",
"C++ files(*.cpp);;C files(*.c);;Hesd files(*.h)");
fileLineEdit->setText(s.toAscii());
}
void Dialog::slotOpenColorDlg()
{
QColor color = QColorDialog::getColor(Qt::blue);
if(color.isValid())
{
colorFrame->setPalette(QPalette(color));
}
}
void Dialog::slotOpenFontDlg()
{
bool ok;
QFont font = QFontDialog::getFont(&ok);
if(ok)
{
fontLineEdit->setFont(font);
}
}
 
main.cpp
 
 
#include <QtGui/QApplication>
#include "dialog.h"
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
 
 
实例.位置函数的使用
 
dialog.h
 
#ifndef DIALOG_H
#define DIALOG_H
 
#include <QDialog>
#include <QLabel>
#include <QGridLayout>
namespace Ui {
class Dialog;
}
 
class Dialog : public QDialog
{
Q_OBJECT
 
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
 
QGridLayout *layout;
QLabel *xLabel;
QLabel *yLabel;
QLabel *frameGeoLabel;
QLabel *posLabel;
QLabel *geoLabel;
QLabel *widthLabel;
QLabel *heightLabel;
QLabel *rectLabel;
QLabel *sizeLabel;
 
void updateLabel();
protected:
void moveEvent(QMoveEvent*);
void resizeEvent(QResizeEvent*);
private:
Ui::Dialog *ui;
};
 
#endif // DIALOG_H

dialog.cpp
 
 
#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
 
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
setWindowTitle("Qing");
layout = new QGridLayout(this);
 
xLabel = new QLabel;
layout->addWidget(xLabel,1,0);
 
yLabel = new QLabel;
layout->addWidget(yLabel,2,0);
 
frameGeoLabel = new QLabel;
layout->addWidget(frameGeoLabel,3,0);
 
posLabel = new QLabel;
layout->addWidget(posLabel,4,0);
 
geoLabel = new QLabel;
layout->addWidget(geoLabel,5,0);
 
widthLabel = new QLabel;
layout->addWidget(widthLabel,6,0);
 
heightLabel = new QLabel;
layout->addWidget(heightLabel,7,0);
 
rectLabel = new QLabel;
layout->addWidget(rectLabel,8,0);
 
sizeLabel = new QLabel;
layout->addWidget(sizeLabel,9,0);
 
layout->setMargin(25);
layout->setSpacing(10);
 
updateLabel();
}
 
Dialog::~Dialog()
{
delete ui;
}
void Dialog::updateLabel()
{
QString temp1,temp2,temp3,temp4;
 
QString str_x;
xLabel->setText("x():"+str_x.setNum(x()));
 
QString str_y;
yLabel->setText("y():"+str_y.setNum(y()));
 
QString frameGeo;
frameGeo = temp1.setNum(frameGeometry().x())+","+
temp2.setNum(frameGeometry().y())+","+
temp3.setNum(frameGeometry().width())+","+
temp4.setNum(frameGeometry().height());
frameGeoLabel->setText("frameGeometry():"+frameGeo);
 
QString position;
position = temp1.setNum(pos().x())+","+temp2.setNum(pos().y());
posLabel->setText("pos():"+position);
 
QString geo;
geo = temp1.setNum(geometry().x())+","+
temp2.setNum(geometry().y())+","+
temp3.setNum(geometry().width())+","+
temp4.setNum(geometry().height());
geoLabel->setText("geometry():"+geo);
 
QString w;
widthLabel->setText("width():"+w.setNum(width()));
 
QString h;
heightLabel->setText("height():"+h.setNum(height()));
 
QString r;
r = temp1.setNum(rect().x())+","+
temp2.setNum(rect().y())+","+
temp3.setNum(rect().width())+","+
temp4.setNum(rect().height());
rectLabel->setText("rect():"+r);
 
QString s;
s = temp1.setNum(size().width())+","+
temp2.setNum(size().height());
sizeLabel->setText("size():"+s);
}
 
void Dialog::moveEvent(QMoveEvent*)
{
updateLabel();
}
 
void Dialog::resizeEvent(QResizeEvent*)
{
updateLabel();
}
 
main.cpp
 
 
#include <QtGui/QApplication>
#include "dialog.h"
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
 
return a.exec();
}
 
 
实例.姓名,性别,年龄,身高对话框
 
dialog.h
 
#ifndef DIALOG_H
 
#define DIALOG_H
 
 
 
#include <QDialog>
 
#include <QLabel>
 
#include <QGridLayout>
 
 
 
namespace Ui {
 
class Dialog;
 
}
 
 
 
class Dialog : public QDialog
 
{
 
Q_OBJECT
 
 
 
public:
 
explicit Dialog(QWidget *parent = 0);
 
~Dialog();
 
 
 
QGridLayout *layout;
 
 
 
QLabel *label1;
 
QLabel *label2;
 
QLabel *label3;
 
QLabel *label4;
 
QLabel *nameLabel;
 
QLabel *sexLabel;
 
QLabel *ageLabel;
 
QLabel *statureLabel;
 
 
 
QPushButton *nameButton;
 
QPushButton *sexButton;
 
QPushButton *ageButton;
 
QPushButton *statureButton;
 
 
 
private:
 
Ui::Dialog *ui;
 
private slots:
 
void slotName();
 
void slotSex();
 
void slotAge();
 
void slotStature();
 
};
 
 
 
#endif // DIALOG_H

dialog.cpp
 
 
 
#include "dialog.h"
 
#include "ui_dialog.h"
 
#include <QtGui>
 
 
 
Dialog::Dialog(QWidget *parent) :
 
QDialog(parent),
 
ui(new Ui::Dialog)
 
{
 
 
 
ui->setupUi(this);
 
setWindowTitle(tr("Qing"));
 
layout = new QGridLayout(this);
 
 
 
label1 = new QLabel(tr("Name:"));
 
label2 = new QLabel(tr("Sex:"));
 
label3 = new QLabel(tr("Age:"));
 
label4 = new QLabel(tr("height:"));
 
 
 
nameLabel = new QLabel(tr("LiMing"));
 
nameLabel->setFrameStyle(QFrame::StyledPanel|QFrame::Plain);
 
sexLabel = new QLabel(tr("男"));
 
sexLabel->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
 
ageLabel = new QLabel(tr("25"));
 
ageLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
 
statureLabel = new QLabel(tr("175.5"));
 
statureLabel->setFrameStyle(QFrame::Box|QFrame::Raised);
 
 
 
nameButton = new QPushButton;
 
nameButton->setIcon(QIcon("G:/UI/images/fish.png"));
 
sexButton = new QPushButton;
 
sexButton->setIcon(QIcon("G:/UI/images/face.png"));
 
ageButton = new QPushButton;
 
//ageButton->setIcon(QIcon("btn.png"));
 
statureButton = new QPushButton;
 
//statureButton->setIcon(QIcon("btn.png"));
 
 
 
 
 
layout->addWidget(label1,1,0);
 
layout->addWidget(label2,2,0);
 
layout->addWidget(label3,3,0);
 
layout->addWidget(label4,4,0);
 
 
 
layout->addWidget(nameLabel,1,1);
 
layout->addWidget(sexLabel,2,1);
 
layout->addWidget(ageLabel,3,1);
 
layout->addWidget(statureLabel,4,1);
 
 
 
layout->addWidget(nameButton,1,3);
 
layout->addWidget(sexButton,2,3);
 
layout->addWidget(ageButton,3,3);
 
layout->addWidget(statureButton,4,3);
 
 
 
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()));
 
}
 
 
 
Dialog::~Dialog()
 
{
 
delete ui;
 
}
 
 
 
void Dialog::slotName()
 
{
 
bool ok;
 
QString name = QInputDialog::getText(this,tr("user name"),
 
tr("please input name"),QLineEdit::Normal,nameLabel->text(),&ok);
 
if(ok & !name.isEmpty())
 
nameLabel->setText(name);
 
}
 
 
 
void Dialog::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 Dialog::slotAge()
 
{
 
bool ok;
 
int age = QInputDialog::getInteger(this,tr("usr age"),
 
tr("Please inpuut age"),ageLabel->text().toInt(),0,150,1,&ok);
 
if(ok)
 
{
 
ageLabel->setText(QString(tr("%1")).arg(age));
 
}
 
}
 
 
 
void Dialog::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 <QtGui/QApplication>
 
#include "dialog.h"
 
#include <QTextCodec>
 
int main(int argc, char *argv[])
 
{
 
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
 
QApplication a(argc, argv);
 
Dialog w;
 
w.show();
 
return a.exec();
 
}
为了使更多的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、付费专栏及课程。

余额充值