安装Qt及Qt Creator
- 在Terminal中输入
sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer
其中
- qt4-dev-tools中包含了Qt Assistant,Qt Linguist,Qt Creator
- qt4-doc 这个是帮助文档
- qt4-qtconfig Qt的配置工具,这个装好默认好
- qt4-demos 官方的一些Demo
- Qt-designer可视化窗体设置工具
- 安装Qt Creator
1.从Qt官方网站上直接下载安装Qt Creator,网址为:http://qt.nokia.com/downloads
2.qt-creator-linux-x86-opensource-2.8.0run(安装包),因为其属性不可执行(可用ls -l命令查看),所以要加上可执行属性(可用chmod命令设置)
3.下载完毕后,直接在终端运行安装包 qt-creator-linux-x86-opensourse 2.8.0run(可用/命令运行)或者,sudo apt-get install qtcreator
要产生标签,则要定义QLabel的类
在图形届满上进行槽与信号的机制
- 按键xxx的属性为objectName ,值可改为:xxx
- 但是界面的值不可更改,若改动,则 编译不过
利用按钮,实现部分代码
在图形界面上加入两个按钮,分别为xxx与yyy,两者属于同一个类,但是有不同的联接,按下xxx可以输出xxx is clicked,按下yyy可以执行别的语句.
将xxx按钮的槽设为clicked(),将yyy按钮的槽设为clicked();
- 在mainwindow.cpp中
void MainWindow::on_pushButton_clicked()
{
cout<<"xxx is clicked"<
- 在mainwindow.h中
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
手动创建槽与信号,将成员函数与信号运用connect将两者绑起来,信号发出,则Qt会自动帮我们调用与信号联接的所有的槽函数
- 添加zzz按钮,并且不设置go to slot.
- 在mainwindow.h中
private slots:
void on_xxx_clicked();
void hello();//自己创建一个成员函数
- 在mainwindow.cpp中
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->zzz,SIGNAL(clicked()),this,SLOT(hello()));//重要
}
void MainWindow::hello()
{
cout<<"hello world"<
结果为按下zzz按钮,按下一次,则输出一次hello world
当按下zzz键时,xxx按钮被设置其"yyyxxx"
- 在mainwindow.cpp中
private slots:
void on_xxx_clicked();
void hello();
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->zzz,SIGNAL(clicked()),this,SLOT(hello()));
}
void MainWindow::hello()
{
ui->xxx->setText("yyyxxx");
}
- 当按下按钮zzz时,则xxx的值被设置为yyyxxx
- 程序运行之前为如下
- 程序运行之后为如下
当在按下按钮zzz时,xxx的值设置为整型时,我们要如何进行类型的转换
- 在mainwindow.h中
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->zzz,SIGNAL(clicked()),this,SLOT(hello()));
}
void MainWindow::hello()
{
int a=10;
QString temp=QString::number(a,10);
ui->xxx->setText(temp);
}
- 则结果为:当按下zzz按钮时,xxx的值为10
- 在程序运行之前
- 在程序运行之后
运用line按钮,当运行之后再框内输入的值,并以文本的形式将输入的内容显示出来
- 头文件中要包含
#include
using namespace std;
#include
- 在mainwindow.h中
private slots:
void on_xxx_clicked();
void hello();
void on_pushButton_clicked();//设置ok键.当输入完成之后,按下ok键则显示出读入的内容
- 在mainwindow.cpp中
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->zzz,SIGNAL(clicked()),this,SLOT(hello()));
}
void MainWindow::hello()
{
QString temp=ui->xxx->text();
qDebug()<lineEdit->text()<
- 运行程序前为如下
- 运行程序后如下,在方框里输入了12345,则以文本的形式显示出来,如下

Paste_Image.png