QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換_第1頁
QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換_第2頁
QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換_第3頁
QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換_第4頁
QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換主要使用QT中的三個方法。

第一個是QString::number(intn,intbase=10);第二個是QString::setNum(shortn,intbase=10);第三個是intQString::toInt(bool*ok=nullptr,intbase=10)const

這三個方法默認(rèn)值都是十進(jìn)制。

先上效果圖,最后會附上源碼:

接下來開始代碼實(shí)現(xiàn):

首先打開QT-新建文件或項(xiàng)目,然后跟著圖中標(biāo)注進(jìn)行下一步

文件名和路徑自己設(shè)置就可。

一直點(diǎn)下一步;

一直點(diǎn)下一步。創(chuàng)建成功先點(diǎn)綠色箭頭運(yùn)行一下。

接著重頭戲來了?。。?!

如圖所示,同時還會在.cpp文件中添加函數(shù)定義:

所要實(shí)現(xiàn)的功能是,當(dāng)點(diǎn)擊對應(yīng)轉(zhuǎn)換為其他進(jìn)制的按鈕時,獲取對應(yīng)輸入框的內(nèi)容,然后把內(nèi)容轉(zhuǎn)換為對應(yīng)進(jìn)制。

主要hao

//QString::number()和setNum()都可以轉(zhuǎn)換

voidMainWindow::on_btn1_clicked()

{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-shi-text();

intvalue=str.toInt();//十進(jìn)制,toInt()默認(rèn)是10進(jìn)制數(shù)

str=str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制

ui-er-setText(str);

str=str.setNum(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制

ui-shiliu-setText(QString("0x%1").arg(str));

str=str.setNum(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn2_clicked()

{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-er-text();//二進(jìn)制

boolok;

intvalue=str.toInt(ok,2);//以二進(jìn)制數(shù)讀入,讀取成功ok=true;

qDebug()"ok="ok;

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-setText(str);

str=QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制

ui-shiliu-setText(QString("0x%1").arg(str));

str=QString::number(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn3_clicked()

{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-shiliu-text();//十六進(jìn)制

boolok;

intvalue=str.toInt(ok,16);//以十六進(jìn)制數(shù)讀入

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-setText(str);

str=str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制

ui-er-setText(str);

str=QString::number(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn4_clicked()

{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-ba-text();//八進(jìn)制

boolok;

intvalue=str.toInt(ok,8);//以八進(jìn)制數(shù)讀入

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-setText(str);

str=str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制

ui-er-setText(str);

str=QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制

ui-shiliu-setText(QString("0x%1").arg(str));

}

好啦,到這里,代碼就結(jié)束啦,是不是感覺很簡單?!

最后附上源碼,親測可運(yùn)行,如果你在運(yùn)行時,出現(xiàn)問題,可以留言。

.pro文件源碼

QT+=coregui

greaterThan(QT_MAJOR_VERSION,4):QT+=widgets

CONFIG+=c++11

#Thefollowingdefinemakesyourcompileremitwarningsifyouuse

#anyQtfeaturethathasbeenmarkeddeprecated(theexactwarnings

#dependonyourcompiler).Pleaseconsultthedocumentationofthe

#deprecatedAPIinordertoknowhowtoportyourcodeawayfromit.

DEFINES+=QT_DEPRECATED_WARNINGS

#YoucanalsomakeyourcodefailtocompileifitusesdeprecatedAPIs.

#Inordertodoso,uncommentthefollowingline.

#YoucanalsoselecttodisabledeprecatedAPIsonlyuptoacertainversionofQt.

#DEFINES+=QT_DISABLE_DEPRECATED_BEFORE=0x060000#disablesalltheAPIsdeprecatedbeforeQt6.0.0

SOURCES+=\

main.cpp\

mainwindow.cpp

HEADERS+=\

mainwindow.h

FORMS+=\

mainwindow.ui

#Defaultrulesfordeployment.

qnx:target.path=/tmp/$${TARGET}/bin

else:unix:!android:target.path=/opt/$${TARGET}/bin

!isEmpty(target.path):INSTALLS+=target

頭文件.h源碼

#ifndefMAINWINDOW_H

#defineMAINWINDOW_H

#includeQMainWindow

QT_BEGIN_NAMESPACE

namespaceUi{classMainWindow;}

QT_END_NAMESPACE

classMainWindow:publicQMainWindow

Q_OBJECT

public:

MainWindow(QWidget*parent=nullptr);

~MainWindow();

privateslots:

voidon_btn1_clicked();

voidon_btn2_clicked();

voidon_btn3_clicked();

voidon_btn4_clicked();

private:

Ui::MainWindow*ui;

#endif//MAINWINDOW_H

main.cpp源碼

#include"mainwindow.h"

#includeQApplication

intmain(intargc,char*argv[])

QApplicationa(argc,argv);

MainWindoww;

w.show();

returna.exec();

}

.cpp源碼

#include"mainwindow.h"

#include"ui_mainwindow.h"

#includeQDebug

MainWindow::MainWindow(QWidget*parent)

:QMainWindow(parent)

,ui(newUi::MainWindow)

ui-setupUi(this);

this-setWindowTitle("各種進(jìn)制之間相互轉(zhuǎn)換");

MainWindow::~MainWindow()

deleteui;

//QString::number()和setNum()都可以轉(zhuǎn)換

voidMainWindow::on_btn1_clicked()

{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-shi-text();

intvalue=str.toInt();//十進(jìn)制,toInt()默認(rèn)是10進(jìn)制數(shù)

str=str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制

ui-er-setText(str);

str=str.setNum(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制

ui-shiliu-setText(QString("0x%1").arg(str));

str=str.setNum(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn2_clicked()

{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-er-text();//二進(jìn)制

boolok;

intvalue=str.toInt(ok,2);//以二進(jìn)制數(shù)讀入,讀取成功ok=true;

qDebug()"ok="ok;

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-setText(str);

str=QString::number(value,16).toUpper();//轉(zhuǎn)為十六進(jìn)制

ui-shiliu-setText(QString("0x%1").arg(str));

str=QString::number(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn3_clicked()

{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-shiliu-text();//十六進(jìn)制

boolok;

intvalue=str.toInt(ok,16);//以十六進(jìn)制數(shù)讀入

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-setText(str);

str=str.setNum(value,2);//轉(zhuǎn)為二進(jìn)制

ui-er-setText(str);

str=QString::number(value,8);//轉(zhuǎn)為八進(jìn)制

ui-ba-setText(str);

voidMainWindow::on_btn4_clicked()

{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制

QStringstr=ui-ba-text();//八進(jìn)制

boolok;

intvalue=str.toInt(ok,8);//以八進(jìn)制數(shù)讀入

str=QString::number(value,10);//轉(zhuǎn)為十進(jìn)制

ui-shi-set

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論