主要使用qt中的三个方法。
- 第一个是qstring::number(int n, int base = 10);
- 第二个是qstring::setnum(short n, int base = 10);
- 第三个是int qstring::toint(bool *ok = nullptr, int base = 10) const
这三个方法默认值都是十进制。
先上效果图,最后会附上源码:
接下来开始代码实现:
首先打开qt->新建文件或项目,然后跟着图中标注进行下一步
文件名和路径自己设置就可。
一直点下一步;
一直点下一步。创建成功先点绿色箭头运行一下。
接着重头戏来了!!!!
如图所示,同时还会在.cpp文件中添加函数定义:
所要实现的功能是,当点击对应“转换为其他进制”的按钮时,获取对应输入框的内容,然后把内容转换为对应进制。
主要hao
//qstring::number()和setnum()都可以转换 void mainwindow::on_btn1_clicked() {//十进制转为其他进制 qstring str = ui->shi->text(); int value = str.toint();//十进制,toint()默认是10进制数 str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = str.setnum(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); str = str.setnum(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn2_clicked() {//二进制转为其他进制 qstring str = ui->er->text();//二进制 bool ok; int value = str.toint(&ok, 2);//以二进制数读入,读取成功ok=true; qdebug() << "ok=" << ok; str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = qstring::number(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); str = qstring::number(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn3_clicked() {//十六进制转为其他进制 qstring str = ui->shiliu->text();//十六进制 bool ok; int value = str.toint(&ok, 16);//以十六进制数读入 str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = qstring::number(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn4_clicked() {//八进制转为其他进制 qstring str = ui->ba->text();//八进制 bool ok; int value = str.toint(&ok, 8);//以八进制数读入 str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = qstring::number(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); }
好啦,到这里,代码就结束啦,是不是感觉很简单?!
最后附上源码,亲测可运行,如果你在运行时,出现问题,可以留言。
.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("各种进制之间相互转换"); } mainwindow::~mainwindow() { delete ui; } //qstring::number()和setnum()都可以转换 void mainwindow::on_btn1_clicked() {//十进制转为其他进制 qstring str = ui->shi->text(); int value = str.toint();//十进制,toint()默认是10进制数 str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = str.setnum(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); str = str.setnum(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn2_clicked() {//二进制转为其他进制 qstring str = ui->er->text();//二进制 bool ok; int value = str.toint(&ok, 2);//以二进制数读入,读取成功ok=true; qdebug() << "ok=" << ok; str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = qstring::number(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); str = qstring::number(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn3_clicked() {//十六进制转为其他进制 qstring str = ui->shiliu->text();//十六进制 bool ok; int value = str.toint(&ok, 16);//以十六进制数读入 str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = qstring::number(value,8);//转为八进制 ui->ba->settext(str); } void mainwindow::on_btn4_clicked() {//八进制转为其他进制 qstring str = ui->ba->text();//八进制 bool ok; int value = str.toint(&ok, 8);//以八进制数读入 str = qstring::number(value,10);//转为十进制 ui->shi->settext(str); str = str.setnum(value,2);//转为二进制 ui->er->settext(str); str = qstring::number(value,16).toupper();//转为十六进制 ui->shiliu->settext(qstring("0x%1").arg(str)); }
运行后的界面如下:
到此这篇关于qt实现二、八、十六进制之间的转换的文章就介绍到这了,更多相关qt进制转换内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!