最近学了学 Qt,想要写个小程序练练手。于是乎,就写了这个用来算乘方的小程序。[aru_17] 现在拿出来给大家看看。
一共写了两个版本,是 C++ 和 Python 版本。[aru_47]
Qt 5.9.3 安装包:
戳我下载PyQt5 安装命令:
- pip install pyqt5 && pip install pyqy5.sip
C++ 版本
目录结构如下。
- 根目录
- | Calculate.pro
- | mainwindow.h
- | main.cpp
- | mainwindow.cpp
- | mainwindow.ui
- | favicon.png
Calculate.pro
#------------------------------------------------- # # Project created by QtCreator 2020-08-13T11:51:55 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Calculate TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as 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 you use 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
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_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(); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QString inp=ui->lineEdit->text(); QString inp2=ui->lineEdit_2->text(); float n=inp.toFloat(); int n2=inp2.toInt(); float s=1; for(int i=1;i<=n2;i++) s*=n; inp.sprintf("Result: %.2f",s); if(!n2 || !n) inp.sprintf("Wrong Format"); ui->label->setText(inp); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>430</width> <height>326</height> </rect> </property> <property name="windowTitle"> <string>Calculate!</string> </property> <property name="windowIcon"> <iconset> <normaloff>favicon.png</normaloff>favicon.png</iconset> </property> <widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="verticalLayoutWidget"> <property name="geometry"> <rect> <x>80</x> <y>90</y> <width>282</width> <height>179</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QLineEdit" name="lineEdit"/> </item> <item> <widget class="QLabel" name="label_3"> <property name="font"> <font> <family>Calibri</family> <pointsize>12</pointsize> </font> </property> <property name="text"> <string>^</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item> <widget class="QLineEdit" name="lineEdit_2"/> </item> <item> <widget class="QPushButton" name="pushButton"> <property name="font"> <font> <family>Consolas</family> <pointsize>10</pointsize> </font> </property> <property name="cursor"> <cursorShape>PointingHandCursor</cursorShape> </property> <property name="text"> <string>Calculate!</string> </property> </widget> </item> <item> <widget class="QLabel" name="label"> <property name="font"> <font> <family>Consolas</family> <pointsize>12</pointsize> </font> </property> <property name="layoutDirection"> <enum>Qt::LeftToRight</enum> </property> <property name="text"> <string>Result:</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item> <widget class="QLabel" name="label_2"> <property name="font"> <font> <family>Consolas</family> <pointsize>11</pointsize> <italic>true</italic> </font> </property> <property name="text"> <string>Warning: The numbers can't be 0!</string> </property> <property name="textFormat"> <enum>Qt::PlainText</enum> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> <widget class="QLabel" name="label_4"> <property name="geometry"> <rect> <x>80</x> <y>40</y> <width>280</width> <height>22</height> </rect> </property> <property name="font"> <font> <family>Consolas</family> <pointsize>14</pointsize> </font> </property> <property name="text"> <string>Involution Calculator by YCH</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
favicon.png
Python 版本
目录结构:
- 根目录
- | main.py
- | mainwindow.ui
- | favicon.png
main.py
import sys from PyQt5 import QtWidgets, uic form_class = uic.loadUiType("mainwindow.ui")[0] class MainWindow(QtWidgets.QMainWindow, form_class): def __init__(self, parent = None): QtWidgets.QMainWindow.__init__(self, parent) self.setupUi(self) self.pushButton.clicked.connect(self.button_clicked) def button_clicked(self): try: n = float(self.lineEdit.text()) n2 = float(self.lineEdit_2.text()) self.label.setText("Result: " + str(n ** n2)) if n == 0 or n2 == 0: self.label.setText("Wrong Format") except: self.label.setText("Wrong Format") app = QtWidgets.QApplication(sys.argv) Window = MainWindow() Window.show() app.exec_()
mainwindow.ui
同 C++ 版本。
favicon.png
同 C++ 版本。
更新完成。[aru_55]
本文作者为Blogych,转载请注明。
WOW!几天没见居然更新了[aru_50]
@光阴似箭现在准备恢复周更 [aru_37]
@杨承翰支持[aru_53]
哇,博主居然会写GUI!厉害啊