QT乱翻书-Spin
作者:互联网
QT乱翻书-Spin
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_btnCal_clicked(); //计算按键 clicked()响应 void on_btnDec_clicked();//读十进制数再用其他进制显示 的按键的 clicked()响应 void on_btnBin_clicked();//读二进制数再用其他进制显示 的按键的 clicked()响应 void on_btnHex_clicked();//读十六进制数再用其他进制显示 的按键的 clicked()响应 private: Ui::Widget *ui; }; #endif // WIDGET_H
/******************************************************************************** ** Form generated from reading UI file 'widget.ui' ** ** Created by: Qt User Interface Compiler version 5.9.1 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_WIDGET_H #define UI_WIDGET_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QDoubleSpinBox> #include <QtWidgets/QGridLayout> #include <QtWidgets/QHeaderView> #include <QtWidgets/QLabel> #include <QtWidgets/QPushButton> #include <QtWidgets/QSpacerItem> #include <QtWidgets/QSpinBox> #include <QtWidgets/QVBoxLayout> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_Widget { public: QVBoxLayout *verticalLayout; QGridLayout *gridLayout; QLabel *label_3; QDoubleSpinBox *spinTotal; QSpinBox *spinNum; QDoubleSpinBox *spinPrice; QLabel *label_2; QLabel *label; QPushButton *btnCal; QSpacerItem *verticalSpacer; QGridLayout *gridLayout_2; QLabel *label_6; QLabel *label_7; QPushButton *btnBin; QLabel *label_8; QPushButton *btnDec; QPushButton *btnHex; QSpacerItem *horizontalSpacer_2; QSpinBox *spinDec; QSpinBox *spinBin; QSpinBox *spinHex; void setupUi(QWidget *Widget) { if (Widget->objectName().isEmpty()) Widget->setObjectName(QStringLiteral("Widget")); Widget->resize(303, 172); QFont font; font.setFamily(QString::fromUtf8("\345\256\213\344\275\223")); font.setPointSize(10); Widget->setFont(font); verticalLayout = new QVBoxLayout(Widget); verticalLayout->setSpacing(6); verticalLayout->setContentsMargins(11, 11, 11, 11); verticalLayout->setObjectName(QStringLiteral("verticalLayout")); gridLayout = new QGridLayout(); gridLayout->setSpacing(6); gridLayout->setObjectName(QStringLiteral("gridLayout")); label_3 = new QLabel(Widget); label_3->setObjectName(QStringLiteral("label_3")); label_3->setAlignment(Qt::AlignCenter); gridLayout->addWidget(label_3, 1, 2, 1, 1); spinTotal = new QDoubleSpinBox(Widget); spinTotal->setObjectName(QStringLiteral("spinTotal")); spinTotal->setMaximum(100000); gridLayout->addWidget(spinTotal, 1, 3, 1, 1); spinNum = new QSpinBox(Widget); spinNum->setObjectName(QStringLiteral("spinNum")); spinNum->setFrame(true); spinNum->setButtonSymbols(QAbstractSpinBox::UpDownArrows); spinNum->setProperty("showGroupSeparator", QVariant(false)); spinNum->setMaximum(1000); spinNum->setValue(4); gridLayout->addWidget(spinNum, 0, 1, 1, 1); spinPrice = new QDoubleSpinBox(Widget); spinPrice->setObjectName(QStringLiteral("spinPrice")); spinPrice->setMinimumSize(QSize(90, 0)); spinPrice->setMaximum(10000); spinPrice->setValue(12.43); gridLayout->addWidget(spinPrice, 0, 3, 1, 1); label_2 = new QLabel(Widget); label_2->setObjectName(QStringLiteral("label_2")); label_2->setAlignment(Qt::AlignCenter); gridLayout->addWidget(label_2, 0, 2, 1, 1); label = new QLabel(Widget); label->setObjectName(QStringLiteral("label")); label->setAlignment(Qt::AlignCenter); gridLayout->addWidget(label, 0, 0, 1, 1); btnCal = new QPushButton(Widget); btnCal->setObjectName(QStringLiteral("btnCal")); gridLayout->addWidget(btnCal, 1, 1, 1, 1); verticalLayout->addLayout(gridLayout); verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); verticalLayout->addItem(verticalSpacer); gridLayout_2 = new QGridLayout(); gridLayout_2->setSpacing(6); gridLayout_2->setObjectName(QStringLiteral("gridLayout_2")); label_6 = new QLabel(Widget); label_6->setObjectName(QStringLiteral("label_6")); label_6->setMinimumSize(QSize(0, 0)); label_6->setAlignment(Qt::AlignCenter); gridLayout_2->addWidget(label_6, 0, 0, 1, 1); label_7 = new QLabel(Widget); label_7->setObjectName(QStringLiteral("label_7")); label_7->setAlignment(Qt::AlignCenter); gridLayout_2->addWidget(label_7, 1, 0, 1, 1); btnBin = new QPushButton(Widget); btnBin->setObjectName(QStringLiteral("btnBin")); gridLayout_2->addWidget(btnBin, 1, 3, 1, 1); label_8 = new QLabel(Widget); label_8->setObjectName(QStringLiteral("label_8")); label_8->setAlignment(Qt::AlignCenter); gridLayout_2->addWidget(label_8, 2, 0, 1, 1); btnDec = new QPushButton(Widget); btnDec->setObjectName(QStringLiteral("btnDec")); gridLayout_2->addWidget(btnDec, 0, 3, 1, 1); btnHex = new QPushButton(Widget); btnHex->setObjectName(QStringLiteral("btnHex")); gridLayout_2->addWidget(btnHex, 2, 3, 1, 1); horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); gridLayout_2->addItem(horizontalSpacer_2, 1, 2, 1, 1); spinDec = new QSpinBox(Widget); spinDec->setObjectName(QStringLiteral("spinDec")); spinDec->setMinimumSize(QSize(100, 0)); spinDec->setMaximum(255); spinDec->setValue(12); gridLayout_2->addWidget(spinDec, 0, 1, 1, 1); spinBin = new QSpinBox(Widget); spinBin->setObjectName(QStringLiteral("spinBin")); spinBin->setMaximum(255); spinBin->setDisplayIntegerBase(2); gridLayout_2->addWidget(spinBin, 1, 1, 1, 1); spinHex = new QSpinBox(Widget); spinHex->setObjectName(QStringLiteral("spinHex")); spinHex->setMaximum(255); spinHex->setValue(12); spinHex->setDisplayIntegerBase(16); gridLayout_2->addWidget(spinHex, 2, 1, 1, 1); verticalLayout->addLayout(gridLayout_2); retranslateUi(Widget); QMetaObject::connectSlotsByName(Widget); } // setupUi void retranslateUi(QWidget *Widget) { Widget->setWindowTitle(QApplication::translate("Widget", "SpinBox\346\265\213\350\257\225", Q_NULLPTR)); label_3->setText(QApplication::translate("Widget", "\346\200\273 \344\273\267", Q_NULLPTR)); spinTotal->setPrefix(QApplication::translate("Widget", "$ ", Q_NULLPTR)); spinNum->setSuffix(QApplication::translate("Widget", " kg", Q_NULLPTR)); spinPrice->setPrefix(QApplication::translate("Widget", "$ ", Q_NULLPTR)); label_2->setText(QApplication::translate("Widget", "\345\215\225 \344\273\267", Q_NULLPTR)); label->setText(QApplication::translate("Widget", "\346\225\260 \351\207\217", Q_NULLPTR)); btnCal->setText(QApplication::translate("Widget", "\350\256\241\347\256\227", Q_NULLPTR)); label_6->setText(QApplication::translate("Widget", "\345\215\201 \350\277\233 \345\210\266", Q_NULLPTR)); label_7->setText(QApplication::translate("Widget", "\344\272\214 \350\277\233 \345\210\266", Q_NULLPTR)); btnBin->setText(QApplication::translate("Widget", "\350\275\254\346\215\242\344\270\272\345\205\266\344\273\226\350\277\233\345\210\266", Q_NULLPTR)); label_8->setText(QApplication::translate("Widget", "\345\215\201\345\205\255\350\277\233\345\210\266", Q_NULLPTR)); btnDec->setText(QApplication::translate("Widget", "\350\275\254\346\215\242\344\270\272\345\205\266\344\273\226\350\277\233\345\210\266", Q_NULLPTR)); btnHex->setText(QApplication::translate("Widget", "\350\275\254\346\215\242\344\270\272\345\205\266\344\273\226\350\277\233\345\210\266", Q_NULLPTR)); spinDec->setSuffix(QString()); spinDec->setPrefix(QApplication::translate("Widget", "Dec ", Q_NULLPTR)); spinBin->setPrefix(QApplication::translate("Widget", "Bin ", Q_NULLPTR)); spinHex->setPrefix(QApplication::translate("Widget", "Hex ", Q_NULLPTR)); } // retranslateUi }; namespace Ui { class Widget: public Ui_Widget {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_WIDGET_H
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
#include "widget.h" #include "ui_widget.h" #include <QString> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); //“数量”和“单价”两个SpinBox的valueChanged()信号与on_btnCal_clicked()槽关联 QObject::connect(ui->spinNum,SIGNAL(valueChanged(int)),this,SLOT(on_btnCal_clicked())); // QObject::connect(ui->spinPrice,SIGNAL(valueChanged(double)),this,SLOT(on_btnCal_clicked())); QObject::connect(ui->spinDec,SIGNAL(valueChanged(int)), this,SLOT(on_btnDec_clicked()));//spinDec 十进制显示的SpinBox QObject::connect(ui->spinBin,SIGNAL(valueChanged(int)), this,SLOT(on_btnBin_clicked()));//spinBin 二进制显示的SpinBox QObject::connect(ui->spinHex,SIGNAL(valueChanged(int)), this,SLOT(on_btnHex_clicked()));//spinHex 十六进制显示的SpinBox } Widget::~Widget() { delete ui; } void Widget::on_btnCal_clicked() { //计算按键 int num=ui->spinNum->value(); //读取数量,直接是整数 float price=ui->spinPrice->value();//读取单价,直接是浮点数 float total=num*price;//相乘计算 ui->spinTotal->setValue(total); //直接显示浮点数 } void Widget::on_btnDec_clicked() { //读取十进制,以其他进制显示 int val; val=ui->spinDec->value();//读取十进制数 ui->spinBin->setValue(val); //设置数值即可,自动以二进制显示 ui->spinHex->setValue(val); //设置数值即可,自动以十六进制显示 } void Widget::on_btnBin_clicked() { //读取二进制,以其他进制显示 int val=ui->spinBin->value();//读取spinBin里的二进制数,得到整数 ui->spinDec->setValue(val);//设置数值即可,自动以十进制显示 ui->spinHex->setValue(val);//设置数值即可,自动以十六进制显示 } void Widget::on_btnHex_clicked() { //读取十六进制,以其他进制显示 int val; val=ui->spinHex->value();//读取 spinHex 里的十六进制数,得到整数 ui->spinDec->setValue(val);//设置数值即可,自动以十进制显示 ui->spinBin->setValue(val);//设置数值即可,自动以二进制显示 }
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Widget</class> <widget class="QWidget" name="Widget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>442</width> <height>238</height> </rect> </property> <property name="font"> <font> <family>宋体</family> <pointsize>10</pointsize> </font> </property> <property name="windowTitle"> <string>SpinBox测试</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QGridLayout" name="gridLayout"> <item row="1" column="2"> <widget class="QLabel" name="label_3"> <property name="text"> <string>总 价</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="3"> <widget class="QDoubleSpinBox" name="spinTotal"> <property name="prefix"> <string>$ </string> </property> <property name="maximum"> <double>100000.000000000000000</double> </property> </widget> </item> <item row="0" column="1"> <widget class="QSpinBox" name="spinNum"> <property name="frame"> <bool>true</bool> </property> <property name="buttonSymbols"> <enum>QAbstractSpinBox::UpDownArrows</enum> </property> <property name="showGroupSeparator" stdset="0"> <bool>false</bool> </property> <property name="suffix"> <string> kg</string> </property> <property name="maximum"> <number>1000</number> </property> <property name="value"> <number>4</number> </property> </widget> </item> <item row="0" column="3"> <widget class="QDoubleSpinBox" name="spinPrice"> <property name="minimumSize"> <size> <width>90</width> <height>0</height> </size> </property> <property name="prefix"> <string>$ </string> </property> <property name="maximum"> <double>10000.000000000000000</double> </property> <property name="value"> <double>12.430000000000000</double> </property> </widget> </item> <item row="0" column="2"> <widget class="QLabel" name="label_2"> <property name="text"> <string>单 价</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>数 量</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="1"> <widget class="QPushButton" name="btnCal"> <property name="text"> <string>计算</string> </property> </widget> </item> </layout> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <layout class="QGridLayout" name="gridLayout_2"> <item row="0" column="0"> <widget class="QLabel" name="label_6"> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="text"> <string>十 进 制</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_7"> <property name="text"> <string>二 进 制</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="3"> <widget class="QPushButton" name="btnBin"> <property name="text"> <string>转换为其他进制</string> </property> </widget> </item> <item row="2" column="0"> <widget class="QLabel" name="label_8"> <property name="text"> <string>十六进制</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="3"> <widget class="QPushButton" name="btnDec"> <property name="text"> <string>转换为其他进制</string> </property> </widget> </item> <item row="2" column="3"> <widget class="QPushButton" name="btnHex"> <property name="text"> <string>转换为其他进制</string> </property> </widget> </item> <item row="1" column="2"> <spacer name="horizontalSpacer_2"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item row="0" column="1"> <widget class="QSpinBox" name="spinDec"> <property name="minimumSize"> <size> <width>100</width> <height>0</height> </size> </property> <property name="suffix"> <string/> </property> <property name="prefix"> <string>Dec </string> </property> <property name="maximum"> <number>255</number> </property> <property name="value"> <number>12</number> </property> </widget> </item> <item row="1" column="1"> <widget class="QSpinBox" name="spinBin"> <property name="prefix"> <string>Bin </string> </property> <property name="maximum"> <number>255</number> </property> <property name="displayIntegerBase"> <number>2</number> </property> </widget> </item> <item row="2" column="1"> <widget class="QSpinBox" name="spinHex"> <property name="prefix"> <string>Hex </string> </property> <property name="maximum"> <number>255</number> </property> <property name="value"> <number>12</number> </property> <property name="displayIntegerBase"> <number>16</number> </property> </widget> </item> </layout> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
标签:setObjectName,Widget,QT,label,ui,翻书,gridLayout,new,Spin 来源: https://www.cnblogs.com/kanbuxiaqu/p/15685735.html