安裝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
Paste_Image.png
- 但是界面的值不可更改,若改動,則 編譯不過
Paste_Image.png
利用按鈕,實現部分代碼
在圖形界面上加入兩個按鈕,分別為xxx與yyy,兩者屬于同一個類,但是有不同的聯接,按下xxx可以輸出xxx is clicked,按下yyy可以執行別的語句.
將xxx按鈕的槽設為clicked(),將yyy按鈕的槽設為clicked();
Paste_Image.png
Paste_Image.png
- 在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();
手動創建槽與信號,將成員函數與信號運用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"<<endl;
}
結果為按下zzz按鈕,按下一次,則輸出一次hello world
Paste_Image.png
當按下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
- 程序運行之前為如下
Paste_Image.png
- 程序運行之后為如下
Paste_Image.png
當在按下按鈕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
- 在程序運行之前
Paste_Image.png
- 在程序運行之后
Paste_Image.png
運用line按鈕,當運行之后再框內輸入的值,并以文本的形式將輸入的內容顯示出來
- 頭文件中要包含
#include <iostream>
using namespace std;
#include <QDebug>
- 在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()<<temp<<endl;
}
void MainWindow::on_pushButton_clicked()
{
qDebug()<<ui->lineEdit->text()<<endl;
}
- 運行程序前為如下
Paste_Image.png
- 運行程序后如下,在方框里輸入了12345,則以文本的形式顯示出來,如下
Paste_Image.png
Paste_Image.png