实例1.helloworld
hello.cpp
#include
#include
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();
}
实例2.文件,颜色,字体对话框
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
#include
#include
#include
#include
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
#include
#include
#include
#include
#include
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
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
实例3.位置函数的使用
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
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
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
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
实例4.姓名,性别,年龄,身高对话框
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
#include
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
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
#include "dialog.h"
#include
int main(int argc, char *argv[])
{
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}