小编在期末数据结构课设中遇到要做可视化界面的问题,特意去学习了一下Qt的用法,今天就来给大家分享一下。
我用的是Qt5.80,当然这只是一个简易的计算器Demo,,请大家勿喷。
首先我创建了一个Qt Widgets Application中的一个对话框项目,注意不用把UI文件勾选。
最终的UI界面是这样的:。
具体实现代码如下:
1 calculator.pro 2 #------------------------------------------------- 3 # 4 # Project created by QtCreator 2018-01-05T14:55:33 5 # 6 #------------------------------------------------- 7 8 QT += core gui 9 10 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 11 12 TARGET = calculator 13 TEMPLATE = app 14 15 # The following define makes your compiler emit warnings if you use 16 # any feature of Qt which as been marked as deprecated (the exact warnings 17 # depend on your compiler). Please consult the documentation of the 18 # deprecated API in order to know how to port your code away from it. 19 DEFINES += QT_DEPRECATED_WARNINGS 20 21 # You can also make your code fail to compile if you use deprecated APIs. 22 # In order to do so, uncomment the following line. 23 # You can also select to disable deprecated APIs only up to a certain version of Qt. 24 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 25 26 27 SOURCES += main.cpp28 calculator.cpp 29 30 HEADERS += calculator.h
1 calculator.cpp 2 #ifndef CALCULATOR_H 3 #define CALCULATOR_H 4 5 #include <QDialog> 6 #include <QPushButton> 7 #include <QLineEdit> 8 #include <QString> 9 class Dialog : public QDialog 10 { 11 Q_OBJECT 12 13 public: 14 Dialog(QWidget *parent = 0); 15 ~Dialog(); 16 17 protected: 18 //声明需要的部件; 19 QPushButton* button_0; 20 QPushButton* button_1; 21 QPushButton* button_2; 22 QPushButton* button_3; 23 QPushButton* button_4; 24 QPushButton* button_5; 25 QPushButton* button_6; 26 QPushButton* button_7; 27 QPushButton* button_8; 28 QPushButton* button_9; 29 QPushButton* button_plus; 30 QPushButton* button_minus; 31 QPushButton* button_multiply; 32 QPushButton* button_devide; 33 QPushButton* button_equal; 34 QPushButton* button_clearAll; 35 36 QLineEdit* lineEdit; 37 QLineEdit* lineEdit_1; 38 39 //声明需要的变量 40 //第一个数,第二个数,运算结果; 41 float num1,num2,result; 42 QString S;//用于显示在lineEdit里面的字符串; 43 //等号运算符的时候,判断是加、减、乘、除的哪一个 44 char sign; 45 int mark;//若点击过加减乘除,mark=2,默认为1; 46 47 //声明处理按钮的槽函数 48 private slots: 49 //按照ui文件默认命名规则 50 void on_button_0_clicked(); 51 void on_button_1_clicked(); 52 void on_button_2_clicked(); 53 void on_button_3_clicked(); 54 void on_button_4_clicked(); 55 void on_button_5_clicked(); 56 void on_button_6_clicked(); 57 void on_button_7_clicked(); 58 void on_button_8_clicked(); 59 void on_button_9_clicked(); 60 61 void on_button_plus_clicked(); 62 void on_button_minus_clicked(); 63 void on_button_multiply_clicked(); 64 void on_button_devide_clicked(); 65 void on_button_equal_clicked(); 66 void on_button_clearAll_clicked(); 67 }; 68 69 #endif // CALCULATOR_H
1 calculator.cpp 2 #include "calculator.h" 3 #include <QGridLayout> 4 5 Dialog::Dialog(QWidget *parent) 6 : QDialog(parent) 7 { 8 //初始化变量 9 num1=0.0; 10 num2=0.0; 11 result=0.0; 12 S="";//显示在编辑框的字符串初始化为空 13 mark=1; //表示没有点击过加减乘除 14 //初始化部件对象指针 15 button_0=new QPushButton("0"); 16 button_1=new QPushButton("1"); 17 button_2=new QPushButton("2"); 18 button_3=new QPushButton("3"); 19 button_4=new QPushButton("4"); 20 button_5=new QPushButton("5"); 21 button_6=new QPushButton("6"); 22 button_7=new QPushButton("7"); 23 button_8=new QPushButton("8"); 24 button_9=new QPushButton("9"); 25 button_plus=new QPushButton("+"); 26 button_minus=new QPushButton("-"); 27 button_multiply=new QPushButton("*"); 28 button_devide=new QPushButton("/"); 29 button_equal=new QPushButton("="); 30 button_clearAll=new QPushButton("c"); 31 32 lineEdit=new QLineEdit("请输入所要计算的值"); 33 lineEdit_1=new QLineEdit("?CL"); 34 //使用网格布局,还有水平和垂直布局 35 QGridLayout *Grid=new QGridLayout; 36 Grid->addWidget(lineEdit,1,1,1,4,Qt::Alignment()); 37 //参数含义:widget部件指针;所在行;所在列;行宽;列宽;对齐方式; 38 Grid->addWidget(lineEdit_1,2,1,1,4,Qt::Alignment()); 39 40 Grid->addWidget(button_0,3,1,Qt::Alignment()); 41 Grid->addWidget(button_1,3,2,Qt::Alignment()); 42 Grid->addWidget(button_2,3,3,Qt::Alignment()); 43 Grid->addWidget(button_3,3,4,Qt::Alignment()); 44 45 Grid->addWidget(button_4,4,1,Qt::Alignment()); 46 Grid->addWidget(button_5,4,2,Qt::Alignment()); 47 Grid->addWidget(button_6,4,3,Qt::Alignment()); 48 Grid->addWidget(button_7,4,4,Qt::Alignment()); 49 50 Grid->addWidget(button_8,5,1,Qt::Alignment()); 51 Grid->addWidget(button_9,5,2,Qt::Alignment()); 52 Grid->addWidget(button_plus,5,3,Qt::Alignment()); 53 Grid->addWidget(button_minus,5,4,Qt::Alignment()); 54 55 Grid->addWidget(button_multiply,6,1,Qt::Alignment()); 56 Grid->addWidget(button_devide,6,2,Qt::Alignment()); 57 Grid->addWidget(button_equal,6,3,Qt::Alignment()); 58 Grid->addWidget(button_clearAll,6,4,Qt::Alignment()); 59 60 //把Grid布局设置给当前创建的对话框对象 61 this->setLayout(Grid); //必不可少 62 63 //实现连接函数 Connect函数参数 (pushbutton,signal,this指针,slot函数) 64 connect(button_0,SIGNAL(clicked(bool)),this,SLOT(on_button_0_clicked())); 65 connect(button_1,SIGNAL(clicked(bool)),this,SLOT(on_button_1_clicked())); 66 connect(button_2,SIGNAL(clicked(bool)),this,SLOT(on_button_2_clicked())); 67 connect(button_3,SIGNAL(clicked(bool)),this,SLOT(on_button_3_clicked())); 68 connect(button_4,SIGNAL(clicked(bool)),this,SLOT(on_button_4_clicked())); 69 connect(button_5,SIGNAL(clicked(bool)),this,SLOT(on_button_5_clicked())); 70 connect(button_6,SIGNAL(clicked(bool)),this,SLOT(on_button_6_clicked())); 71 connect(button_7,SIGNAL(clicked(bool)),this,SLOT(on_button_7_clicked())); 72 connect(button_8,SIGNAL(clicked(bool)),this,SLOT(on_button_8_clicked())); 73 connect(button_9,SIGNAL(clicked(bool)),this,SLOT(on_button_9_clicked())); 74 75 connect(button_plus,SIGNAL(clicked(bool)),this,SLOT(on_button_plus_clicked())); 76 connect(button_minus,SIGNAL(clicked(bool)),this,SLOT(on_button_minus_clicked())); 77 connect(button_multiply,SIGNAL(clicked(bool)),this,SLOT(on_button_multiply_clicked())); 78 connect(button_devide,SIGNAL(clicked(bool)),this,SLOT(on_button_devide_clicked())); 79 connect(button_equal,SIGNAL(clicked(bool)),this,SLOT(on_button_equal_clicked())); 80 connect(button_clearAll,SIGNAL(clicked(bool)),this,SLOT(on_button_clearAll_clicked())); 81 82 } 83 84 Dialog::~Dialog() 85 { 86 87 } 88 89 //实现槽函数 90 void Dialog::on_button_0_clicked() 91 { 92 S+="0"; 93 lineEdit->setText(S);//把数据设置到编辑框 94 95 if(mark==1) 96 { 97 num1=num1*10;//继续添0 98 } 99 else 100 { 101 num2=num2*10; 102 } 103 } 104 void Dialog::on_button_1_clicked() 105 { 106 S+="1"; 107 lineEdit->setText(S); 108 if(mark==1) 109 { 110 num1=num1*10+1; 111 } 112 else 113 { 114 num2=num2*10+1; 115 } 116 } 117 void Dialog::on_button_2_clicked() 118 { 119 S+="2"; 120 lineEdit->setText(S); 121 if(mark==1) 122 { 123 num1=num1*10+2; 124 } 125 else 126 { 127 num2=num2*10+2; 128 } 129 } 130 void Dialog::on_button_3_clicked() 131 { 132 S+="3"; 133 lineEdit->setText(S); 134 if(mark==1) 135 { 136 num1=num1*10+3; 137 } 138 else 139 { 140 num2=num2*10+3; 141 } 142 } 143 void Dialog::on_button_4_clicked() 144 { 145 S+="4"; 146 lineEdit->setText(S); 147 if(mark==1) 148 { 149 num1=num1*10+4; 150 } 151 else 152 { 153 num2=num2*10+4; 154 } 155 } 156 void Dialog::on_button_5_clicked() 157 { 158 S+="5"; 159 lineEdit->setText(S); 160 if(mark==1) 161 { 162 num1=num1*10+5; 163 } 164 else 165 { 166 num2=num2*10+5; 167 } 168 } 169 void Dialog::on_button_6_clicked() 170 { 171 S+="6"; 172 lineEdit->setText(S); 173 if(mark==1) 174 { 175 num1=num1*10+6; 176 } 177 else 178 { 179 num2=num2*10+6; 180 } 181 } 182 void Dialog::on_button_7_clicked() 183 { 184 S+="7"; 185 lineEdit->setText(S); 186 if(mark==1) 187 { 188 num1=num1*10+7; 189 } 190 else 191 { 192 num2=num2*10+7; 193 } 194 } 195 void Dialog::on_button_8_clicked() 196 { 197 S+="8"; 198 lineEdit->setText(S); 199 if(mark==1) 200 { 201 num1=num1*10+8; 202 } 203 else 204 { 205 num2=num2*10+8; 206 } 207 } 208 void Dialog::on_button_9_clicked() 209 { 210 S+="9"; 211 lineEdit->setText(S); 212 if(mark==1) 213 { 214 num1=num1*10+9; 215 } 216 else 217 { 218 num2=num2*10+9; 219 } 220 } 221 void Dialog::on_button_plus_clicked() 222 { 223 S+="+"; 224 sign=‘+‘;//设置标记为+ 225 mark=2; //让其他按钮知道下一个点击的数累加到num2 226 lineEdit->setText(S); 227 } 228 void Dialog::on_button_minus_clicked() 229 { 230 S+="-"; 231 sign=‘-‘; 232 mark=2; 233 lineEdit->setText(S); 234 } 235 void Dialog::on_button_multiply_clicked() 236 { 237 S+="*"; 238 sign=‘*‘; 239 mark=2; 240 lineEdit->setText(S); 241 } 242 void Dialog::on_button_devide_clicked() 243 { 244 S+="/"; 245 sign=‘/‘; 246 mark=2; 247 lineEdit->setText(S); 248 } 249 void Dialog::on_button_equal_clicked() 250 { 251 S+="="; 252 switch(sign) 253 { 254 case ‘+‘: 255 result=num1+num2; 256 break; 257 case ‘-‘: 258 result=num1-num2; 259 break; 260 case ‘*‘: 261 result=num1*num2; 262 break; 263 case ‘/‘: 264 result=num1/num2; 265 break; 266 default: 267 break; 268 } 269 //S+=QString::number(result,10);//该函数result必须为int型 270 // S+=QString("%1").arg(result);//float转QString ,S+=保留之前的式子; 271 //S+=result+‘0‘;result大于等于10时报错 272 S=QString("%1").arg(result); 273 lineEdit_1->setText(S); 274 } 275 void Dialog::on_button_clearAll_clicked() 276 { 277 S=""; 278 //S+="";无法清除 279 lineEdit->setText(S); 280 lineEdit_1->setText(S); 281 mark=1; 282 num1=0.0; 283 num2=0.0; 284 result=0.0; 285 }
1 calculator.cpp 2 #include "calculator.h" 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 Dialog w; 9 w.show(); 10 11 return a.exec(); 12 }
本Demo中没有用到Qt designer,但是部件的声明和槽函数的声明都是按照designer的默认规则书写的。大体的思路是这样的:在构思好总体的UI布局后,在头文件中声明所需要的部件,不管是按钮、文本编辑框,还是行编辑框,都大同小异;之后在在cpp文件中实现相应的功能。该简易的计算器Demo只能满足两个运算数(原谅小编为小白),通过字符串的操作将0-9,+-*/显示在行编辑框内,最后在用户点击等号时,将float型的结果转化为QString输出。文中还使用了网格布局,在使用时千万不可忘记把布局格式设置给当前所创建的对话框对象(this->setLayout(Grid))。