" />

亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換

 更新時(shí)間:2022年05月15日 15:55:54   作者:麥子穗  
本文主要介紹了QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

主要使用QT中的三個(gè)方法。

  • 第一個(gè)是QString::number(int n, int base = 10);
  • 第二個(gè)是QString::setNum(short n, int base = 10);
  • 第三個(gè)是int QString::toInt(bool *ok = nullptr, int base = 10) const

這三個(gè)方法默認(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)行一下。

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

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

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

主要hao

//QString::number()和setNum()都可以轉(zhuǎn)換
void MainWindow::on_btn1_clicked()
{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->shi->text();
    int value = 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);
}
 
void MainWindow::on_btn2_clicked()
{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->er->text();//二進(jìn)制
    bool ok;
    int value = 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);
}
 
void MainWindow::on_btn3_clicked()
{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->shiliu->text();//十六進(jìn)制
    bool ok;
    int value = 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);
}
 
void MainWindow::on_btn4_clicked()
{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->ba->text();//八進(jìn)制
    bool ok;
    int value = 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)行時(shí),出現(xiàn)問題,可以留言。

.pro 文件源碼

QT       += core gui
 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
CONFIG += c++11
 
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
SOURCES += \
    main.cpp \
    mainwindow.cpp
 
HEADERS += \
    mainwindow.h
 
FORMS += \
    mainwindow.ui
 
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

頭文件.h源碼

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
 
private slots:
 
    void on_btn1_clicked();
 
    void on_btn2_clicked();
 
    void on_btn3_clicked();
 
    void on_btn4_clicked();
 
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

main.cpp源碼

#include "mainwindow.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

.cpp源碼

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("各種進(jìn)制之間相互轉(zhuǎn)換");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
//QString::number()和setNum()都可以轉(zhuǎn)換
void MainWindow::on_btn1_clicked()
{//十進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->shi->text();
    int value = 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);
}
 
void MainWindow::on_btn2_clicked()
{//二進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->er->text();//二進(jìn)制
    bool ok;
    int value = 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);
}
 
void MainWindow::on_btn3_clicked()
{//十六進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->shiliu->text();//十六進(jìn)制
    bool ok;
    int value = 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);
}
 
void MainWindow::on_btn4_clicked()
{//八進(jìn)制轉(zhuǎn)為其他進(jìn)制
    QString str = ui->ba->text();//八進(jìn)制
    bool ok;
    int value = 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));
}

運(yùn)行后的界面如下:

 到此這篇關(guān)于QT實(shí)現(xiàn)二、八、十六進(jìn)制之間的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)QT進(jìn)制轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論