做乘方计算的 Qt 小程序

Blogych 3,883 4

最近学了学 Qt,想要写个小程序练练手。于是乎,就写了这个用来算乘方的小程序。[aru_17] 现在拿出来给大家看看。

一共写了两个版本,是 C++ 和 Python 版本。[aru_47]

温馨提示

C++ 版本需要用 Qt 5.9.3,官网:download.qt.io ;Python 版本要用 Python 3 和 PyQt5,官网 python.org

Qt 5.9.3 安装包:

戳我下载

PyQt5 安装命令:

  1. pip install pyqt5 && pip install pyqy5.sip  

C++ 版本

目录结构如下。

  1. 根目录    
  2.  |    Calculate.pro    
  3.  |    mainwindow.h    
  4.  |    main.cpp    
  5.  |    mainwindow.cpp    
  6.  |    mainwindow.ui    
  7.  |    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

做乘方计算的 Qt 小程序

Python 版本

目录结构:

  1. 根目录    
  2.  |    main.py    
  3.  |    mainwindow.ui    
  4.  |    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]

发表评论 取消回复
表情 图片 链接 代码

  1. 光阴似箭
    光阴似箭 Lv 3

    WOW!几天没见居然更新了[aru_50]

  2. 凉秋影
    凉秋影 Lv 1

    哇,博主居然会写GUI!厉害啊

分享