环境安装
1:安装qt-opensource-windows-x86-msvc2010-5.5.0
2:安装vs2010
3:安装vs2010 sp1补丁
4:设置qt的环境变量,并在vs2010的qt选项中选择qt option,添加版本和安装路径。
控件变量获取篇
本人作为小白,在获取控件变量时苦恼半天,在打开ui界面并添加控件后,再回到vs2010下面,无法获取控件的变量名。方法是:在ui designer中编辑好界面的控件后,回到vs2010时,在解决方案管理器中找到该ui文件,鼠标右键并编译,此时ui类中便有各个控件的变量名,便可以对此控件进行相应操作。
信号与槽
1、若不编写UI,在vs2010的类中手动定义控件,此时需要声明信号和槽函数,并调用静态的connect的函数对信号和槽函数进行连接。信号函数可以只声明不定义。
头文件
#include <QtWidgets/QMainWindow>
#include "ui_qqsimple.h"
#include<qlabel.h>
#include<qpushbutton.h>
#include<qstring.h>
class qqsimple : public QMainWindow
{
Q_OBJECT
public:
qqsimple(QWidget *parent = 0);
~qqsimple();
private:
Ui::qqsimpleClass ui;
QLabel *lable;
QPushButton *button;
public slots: //自定义槽函数
void currentTime();
signals: //信号函数
void getData(QString);
};
#endif // QQSIMPLE_H
cpp文件
#include "qqsimple.h"
qqsimple::qqsimple(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->resize(300,400);
lable = new QLabel("lable",this);
button = new QPushButton("get time",this);
lable->move(150,200);
button->move(125,300);
connect(button,SIGNAL(clicked()),this,SLOT(currentTime()));
connect(this,SIGNAL(getData(QString)),lable,SLOT(setText(QString)));//此两个位静态连接函数
}
qqsimple::~qqsimple()
{
delete lable;
delete button;
}
void qqsimple::currentTime()
{
emit getData("this is me");
}
2、编写UI,在连接信号与槽时,快捷键F4,然后从控件的信号发送方单击鼠标右键拖动都槽函数的接收方,注意,此时槽函数一般是自定义的函数名。在定义好函数名并保存UI后,需要返回vs2010中类的头文件中声明此槽函数,并在cpp文件中定义此槽函数,此时信号与槽便连接上了,在槽函数中可以实现自己想要的功能。
UI界面如图:
头文件:
#ifndef MYPLAYER_H
#define MYPLAYER_H
#include <QtWidgets/QMainWindow>
#include "ui_myplayer.h"
class MyPlayer : public QMainWindow
{
Q_OBJECT
public:
MyPlayer(QWidget *parent = 0);
~MyPlayer();
private:
Ui::MyPlayerClass ui;
public slots:
void playpause();
void addFiles();
};
#endif // MYPLAYER_H
cpp文件:
#include "myplayer.h"
#include<qmessagebox.h>
MyPlayer::MyPlayer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
MyPlayer::~MyPlayer()
{
}
void MyPlayer::playpause()
{
QMessageBox msg;
msg.setText("Hello Wiorld!");
msg.exec();
}
void MyPlayer::addFiles()
{
ui.MyLable->setText("this is QT");
}
PS:是纯小白的经验,大神请忽略!!