安裝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的配置工具,這個裝好默認(rèn)好
- qt4-demos 官方的一些Demo
- Qt-designer可視化窗體設(shè)置工具
- 安裝Qt Creator
1.從Qt官方網(wǎng)站上直接下載安裝Qt Creator,網(wǎng)址為:http://qt.nokia.com/downloads
2.qt-creator-linux-x86-opensource-2.8.0run(安裝包),因為其屬性不可執(zhí)行(可用ls -l命令查看),所以要加上可執(zhí)行屬性(可用chmod命令設(shè)置)
3.下載完畢后,直接在終端運行安裝包 qt-creator-linux-x86-opensourse 2.8.0run(可用/命令運行)或者,sudo apt-get install qtcreator
要產(chǎn)生標(biāo)簽,則要定義QLabel的類
在圖形屆滿上進(jìn)行槽與信號的機制
- 按鍵xxx的屬性為objectName ,值可改為:xxx
- 但是界面的值不可更改,若改動,則 編譯不過
利用按鈕,實現(xiàn)部分代碼
在圖形界面上加入兩個按鈕,分別為xxx與yyy,兩者屬于同一個類,但是有不同的聯(lián)接,按下xxx可以輸出xxx is clicked,按下yyy可以執(zhí)行別的語句.
將xxx按鈕的槽設(shè)為clicked(),將yyy按鈕的槽設(shè)為clicked();
- 在mainwindow.cpp中
void MainWindow::on_pushButton_clicked()
{
cout<<"xxx is clicked"<<endl;
}
void MainWindow::on_pushButton_2_clicked()
{
cout<<"hello world"<<endl;
}
- 在mainwindow.h中
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
手動創(chuàng)建槽與信號,將成員函數(shù)與信號運用connect將兩者綁起來,信號發(fā)出,則Qt會自動幫我們調(diào)用與信號聯(lián)接的所有的槽函數(shù)
- 添加zzz按鈕,并且不設(shè)置go to slot.
- 在mainwindow.h中
private slots:
void on_xxx_clicked();
void hello();//自己創(chuàng)建一個成員函數(shù)
- 在mainwindow.cpp中
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->zzz,SIGNAL(clicked()),this,SLOT(hello()));//重要
}
void MainWindow::hello()
{
cout<<"hello world"<<endl;
}
結(jié)果為按下zzz按鈕,按下一次,則輸出一次hello world
當(dāng)按下zzz鍵時,xxx按鈕被設(shè)置其"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");
}
- 當(dāng)按下按鈕zzz時,則xxx的值被設(shè)置為yyyxxx
- 程序運行之前為如下
- 程序運行之后為如下
當(dāng)在按下按鈕zzz時,xxx的值設(shè)置為整型時,我們要如何進(jìn)行類型的轉(zhuǎn)換
- 在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);
}
- 則結(jié)果為:當(dāng)按下zzz按鈕時,xxx的值為10
- 在程序運行之前
- 在程序運行之后
運用line按鈕,當(dāng)運行之后再框內(nèi)輸入的值,并以文本的形式將輸入的內(nèi)容顯示出來
- 頭文件中要包含
#include <iostream>
using namespace std;
#include <QDebug>
- 在mainwindow.h中
private slots:
void on_xxx_clicked();
void hello();
void on_pushButton_clicked();//設(shè)置ok鍵.當(dāng)輸入完成之后,按下ok鍵則顯示出讀入的內(nèi)容
- 在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()<<temp<<endl;
}
void MainWindow::on_pushButton_clicked()
{
qDebug()<<ui->lineEdit->text()<<endl;
}
- 運行程序前為如下
- 運行程序后如下,在方框里輸入了12345,則以文本的形式顯示出來,如下