创建一个项目,基类选择QWidget,类名用Widget。创建完成后进入widget.ui界面。
点击主页面,找到右下角的minimumSize大小和geometry里面的大小,都设置800x600。
从左侧栏拖出一个Label控件和四个PushButton控件,Label控件设置窗口最小值和geometry的大小都为300x100,
PushButton控件都设置为200x50。右键Label控件,选择“改变样式表”,在窗口中输入font: 57 36pt "Ubuntu";Label文本更改为“Qt编程示例”,按钮的文本改为”Mykeyboard”、”To Dialog”、”Result”、”Quit”如图所示
从左侧栏拖出一个Vertical Layout,把四个按钮放进去,然后在右下角的vertical layout属性设置修改为如图所示
从左侧栏拖出两个Horizontal Spacer放在左右两边,然后把两个个Horizontal Spacer 和 Label 以及刚才的Vertical Layout框全部选中,点击主页面上面的 这个按钮。右下角的layoutSpacing设置为50,完成后如图
再从左侧拖入两个Vertical Spacer放在上下两边,然后选中两个Vertical Spacer和中间这个Horizontal Spacer大框,点击上面的垂直布局按钮。最后,找块主页面的空白地方,再次点击上面的垂直布局或者水平布局(这步不可少)。完成后如下图
为使中文不显示乱码,可以在main.cpp中加入
按前面说过的方法添加资源文件,在widget.cpp中使用如下代码给widget加入图片背景
1.QPixmap pixmap(":/img5.png");
2.QPalette palette = this->palette();
3.palette.setBrush(QPalette::Background,QBrush(pixmap.scaled(QApplication::desktop()->size())));
4.this->setPalette(palette);
第三行用到了desktop的函数要引入
运行
在刚才的项目中添加新文件,选择Qt设计师界面类,界面模板选择Dialog without Buttons,类名用Dialog,拖入一个Label,然后按照前面的方法进行格式设置(窗口大小,布局,字体等),完成后如下图
按照前面的代码在dialog.cpp中也加入一个图片背景。
在widget.h中加入#include "dialog.h"和一个成员Dialog *mydialog,在widget.cpp的构造函数中加入mydialog = new Dialog(this) ;代码
然后进入widget.ui中,右键To Dialog按钮,选择转到槽,选择clicked(),确定后在跳转的函数体中加入mydialog->show();
此时我们运行程序,点击To Dialog按钮就能出现Dialog的窗口了。
继续在widget.ui中,右键Quit按钮,转到槽,选择clicked(),在函数体添加close();
此时运行程序,点击Quit按钮就能关闭窗口了。
继续在刚才的项目中添加新文件,选择Qt设计师界面类,界面模板选择Dialog without Buttons,类名用Mykeyboard。
软键盘就是供鼠标点击使用的键盘,整体思想就是信号与槽实现按下某个键时(鼠标按下某个按钮),文本输入框上显示出当前按下的键。
当按下确定键时,将文本显示在Result窗口中。
mykeyboard.h中的代码
1.#ifndef MYKEYBOARD_H
2.#define MYKEYBOARD_H
3.
4.#include
5.#include
6.#include
7.
8.namespace Ui {
9.class Mykeyboard;
10.}
11.
12.class Mykeyboard : public QDialog
13.{
14. Q_OBJECT
15.
16.public:
17. explicit Mykeyboard(QWidget *parent = 0);
18. ~Mykeyboard();
19. friend class Widget ;
20.
21.private:
22. Ui::Mykeyboard *ui;
23. QToolButton *SoftKey[40] ;//所有的软键盘按钮
24. QLineEdit *Line ;//文本输入框
25. QString CurString ;//按下确定后保存再CurString里
26.
27.private slots:
28. void Init() ;
29. void Click1() ;
30. void Click2() ;
31. void Click3() ;
32. void Click4() ;
33. void Click5() ;
34. void Click6() ;
35. void Click7() ;
36. void Click8() ;
37. void Click9() ;
38. void Click0() ;
39. void ClickQ() ;
40. void ClickW() ;
41. void ClickE() ;
42. void ClickR() ;
43. void ClickT() ;
44. void ClickY() ;
45. void ClickU() ;
46. void ClickI() ;
47. void ClickO() ;
48. void ClickP() ;
49. void ClickA() ;
50. void ClickS() ;
51. void ClickD() ;
52. void ClickF() ;
53. void ClickG() ;
54. void ClickH() ;
55. void ClickJ() ;
56. void ClickK() ;
57. void ClickL() ;
58. void ClickBack() ;
59. void ClickClose() ;
60. void ClickZ() ;
61. void ClickX() ;
62. void ClickC() ;
63. void ClickV() ;
64. void ClickB() ;
65. void ClickN() ;
66. void ClickM() ;
67. void ClickPoint() ;
68. void ClickEnter() ;
69.};
70.
#endif // MYKEYBOARD_H
Mykeyboard.cpp中的代码
1.#include "mykeyboard.h"
2.#include "ui_mykeyboard.h"
3.#include
4.
5.Mykeyboard::Mykeyboard(QWidget *parent) :
6. QDialog(parent),
7. ui(new Ui::Mykeyboard)
8.{
9. ui->setupUi(this);
10. setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
11. //WindowStaysOnTopHint设置窗口再最上端,FramelessWindowHint设置无边窗口
12. this->setFixedSize(QApplication::desktop()->width()/2,QApplication::desktop()->height()/2);//设置键盘的窗口大小
13. QString Toolbuttonformat = "QToolButton{background-color:rgba(242,141,102,255);\
14. color:rgba(50,25,25,255);\
15. border:2px groove rgb(242,141,102);border-radius:20px;padding:2px 2px;\
16. background-color: qconicalgradient(cx:0.5, cy:0.522909, angle:179.9, stop:0.494318 rgba(242,141,102,255), \
17. stop:0.5 rgba(242,141,102,255));}\
18. QToolButton:pressed{background-color:rgba(200,200,200,255);}";//把待会儿要用的窗口样式用字符串保存
19.
20. Line = new QLineEdit(this) ;
21. QFont fontalnum("Microsoft YaHei",this->width()/30,50);
22. QFont fontfun("Microsoft YaHei",this->width()/35,50);
23. for(int i = 0 ; i < 40 ; ++i)
24. SoftKey[i] = new QToolButton(this) ;
25. char tmp[2] = {0} ;
26. char Alnum1[30] = "1234567890QWERTYUIOPASDFGHJKL";
27. char Alnum2[8] = "ZXCVBNM" ;
28. for(int i = 0 ; i < 29 ; ++i )
29. {
30. tmp[0] = Alnum1[i] ;
31. SoftKey[i]->setText(tmp) ;//设置文本
32. SoftKey[i]->setFont(fontalnum);//设置字体样式
33. }
34. int j = 0 ;
35. for(int i = 31; i < 38 && Alnum2[j] != '\0' ; ++i,++j )
36. {
37. tmp[0] = Alnum2[j] ;
38. SoftKey[i]->setText(tmp);
39. SoftKey[i]->setFont(fontalnum);
40. }
41.
42. for(int i = 0 ; i < 40 ; ++i)
43. SoftKey[i]->setStyleSheet(Toolbuttonformat);//设置窗口样式
44.
45. Line->setFixedSize(this->width()/1.2,this->height()/4);
46. Line->setStyleSheet("background-color:rgba(247,192,193,255);");
47. Line->setFont(fontalnum) ;
48.
49. for(int i = 0 ; i < 40 ; ++i)
50. SoftKey[i]->resize(this->width()/10,this->height()/6);
51.
52. Line->move((this->width()-Line->width())/2,(this->height()-4*SoftKey[0]->height()-Line->height())/2);
53.
54. int k = 0 ;
55. for( int i = 0 ; i < 4 ; ++i)
56. {
57. for( j = 0 ; j < 10 ; ++j)
58. {
59. SoftKey[k++]->move(j*SoftKey[i]->width(),(i+2)*SoftKey[i]->height());
60. }
61. }
62.
63. SoftKey[29]->setIcon(QIcon(":/Backspace.png"));//给按钮对象添加图标
64. SoftKey[29]->setToolButtonStyle(Qt::ToolButtonIconOnly);//设置按钮只有图标没有文本
65. SoftKey[29]->setIconSize(QSize(SoftKey[29]->width()/2,SoftKey[29]->height()/2));
66. SoftKey[30]->setText(QObject::tr("关闭"));
67. SoftKey[30]->setFont(fontfun);
68. SoftKey[38]->setText(QObject::tr("."));
69. SoftKey[38]->setFont(fontalnum);
70. SoftKey[39]->setText(QObject::tr("确定"));
71. SoftKey[39]->setFont(fontfun);
72. Init();
73.}
74.
75.void Mykeyboard::Init()
76.{
77. connect(SoftKey[0],SIGNAL(clicked(bool)),this,SLOT(Click1())) ;
78. connect(SoftKey[1],SIGNAL(clicked(bool)),this,SLOT(Click2())) ;
79. connect(SoftKey[2],SIGNAL(clicked(bool)),this,SLOT(Click3())) ;
80. connect(SoftKey[3],SIGNAL(clicked(bool)),this,SLOT(Click4())) ;
81. connect(SoftKey[4],SIGNAL(clicked(bool)),this,SLOT(Click5())) ;
82. connect(SoftKey[5],SIGNAL(clicked(bool)),this,SLOT(Click6())) ;
83. connect(SoftKey[6],SIGNAL(clicked(bool)),this,SLOT(Click7())) ;
84. connect(SoftKey[7],SIGNAL(clicked(bool)),this,SLOT(Click8())) ;
85. connect(SoftKey[8],SIGNAL(clicked(bool)),this,SLOT(Click9())) ;
86. connect(SoftKey[9],SIGNAL(clicked(bool)),this,SLOT(Click0())) ;
87. connect(SoftKey[10],SIGNAL(clicked(bool)),this,SLOT(ClickQ())) ;
88. connect(SoftKey[11],SIGNAL(clicked(bool)),this,SLOT(ClickW())) ;
89. connect(SoftKey[12],SIGNAL(clicked(bool)),this,SLOT(ClickE())) ;
90. connect(SoftKey[13],SIGNAL(clicked(bool)),this,SLOT(ClickR())) ;
91. connect(SoftKey[14],SIGNAL(clicked(bool)),this,SLOT(ClickT())) ;
92. connect(SoftKey[15],SIGNAL(clicked(bool)),this,SLOT(ClickY())) ;
93. connect(SoftKey[16],SIGNAL(clicked(bool)),this,SLOT(ClickU())) ;
94. connect(SoftKey[17],SIGNAL(clicked(bool)),this,SLOT(ClickI())) ;
95. connect(SoftKey[18],SIGNAL(clicked(bool)),this,SLOT(ClickO())) ;
96. connect(SoftKey[19],SIGNAL(clicked(bool)),this,SLOT(ClickP())) ;
97. connect(SoftKey[20],SIGNAL(clicked(bool)),this,SLOT(ClickA())) ;
98. connect(SoftKey[21],SIGNAL(clicked(bool)),this,SLOT(ClickS())) ;
99. connect(SoftKey[22],SIGNAL(clicked(bool)),this,SLOT(ClickD())) ;
100. connect(SoftKey[23],SIGNAL(clicked(bool)),this,SLOT(ClickF())) ;
101. connect(SoftKey[24],SIGNAL(clicked(bool)),this,SLOT(ClickG())) ;
102. connect(SoftKey[25],SIGNAL(clicked(bool)),this,SLOT(ClickH())) ;
103. connect(SoftKey[26],SIGNAL(clicked(bool)),this,SLOT(ClickJ())) ;
104. connect(SoftKey[27],SIGNAL(clicked(bool)),this,SLOT(ClickK())) ;
105. connect(SoftKey[28],SIGNAL(clicked(bool)),this,SLOT(ClickL())) ;
106. connect(SoftKey[29],SIGNAL(clicked(bool)),this,SLOT(ClickBack())) ;
107. connect(SoftKey[30],SIGNAL(clicked(bool)),this,SLOT(ClickClose())) ;
108. connect(SoftKey[31],SIGNAL(clicked(bool)),this,SLOT(ClickZ())) ;
109. connect(SoftKey[32],SIGNAL(clicked(bool)),this,SLOT(ClickX())) ;
110. connect(SoftKey[33],SIGNAL(clicked(bool)),this,SLOT(ClickC())) ;
111. connect(SoftKey[34],SIGNAL(clicked(bool)),this,SLOT(ClickV())) ;
112. connect(SoftKey[35],SIGNAL(clicked(bool)),this,SLOT(ClickB())) ;
113. connect(SoftKey[36],SIGNAL(clicked(bool)),this,SLOT(ClickN())) ;
114. connect(SoftKey[37],SIGNAL(clicked(bool)),this,SLOT(ClickM())) ;
115. connect(SoftKey[38],SIGNAL(clicked(bool)),this,SLOT(ClickPoint())) ;
116. connect(SoftKey[39],SIGNAL(clicked(bool)),this,SLOT(ClickEnter())) ;
117.}
118.
119.void Mykeyboard::Click1()
120.{
121. Line->setText(Line->text()+"1");
122.}
123.void Mykeyboard::Click2()
124.{
125. Line->setText(Line->text()+"2");
126.}
127.void Mykeyboard::Click3()
128.{
129. Line->setText(Line->text()+"3");
130.}
131.void Mykeyboard::Click4()
132.{
133. Line->setText(Line->text()+"4");
134.}
135.void Mykeyboard::Click5()
136.{
137. Line->setText(Line->text()+"5");
138.}
139.void Mykeyboard::Click6()
140.{
141. Line->setText(Line->text()+"6");
142.}
143.void Mykeyboard::Click7()
144.{
145. Line->setText(Line->text()+"7");
146.}
147.void Mykeyboard::Click8()
148.{
149. Line->setText(Line->text()+"8");
150.}
151.void Mykeyboard::Click9()
152.{
153. Line->setText(Line->text()+"9");
154.}
155.void Mykeyboard::Click0()
156.{
157. Line->setText(Line->text()+"0");
158.}
159.void Mykeyboard::ClickQ()
160.{
161. Line->setText(Line->text()+"Q");
162.}
163.void Mykeyboard::ClickW()
164.{
165. Line->setText(Line->text()+"W");
166.}
167.void Mykeyboard::ClickE()
168.{
169. Line->setText(Line->text()+"E");
170.}
171.void Mykeyboard::ClickR()
172.{
173. Line->setText(Line->text()+"R");
174.}
175.void Mykeyboard::ClickT()
176.{
177. Line->setText(Line->text()+"T");
178.}
179.void Mykeyboard::ClickY()
180.{
181. Line->setText(Line->text()+"Y");
182.}
183.void Mykeyboard::ClickU()
184.{
185. Line->setText(Line->text()+"U");
186.}
187.void Mykeyboard::ClickI()
188.{
189. Line->setText(Line->text()+"I");
190.}
191.void Mykeyboard::ClickO()
192.{
193. Line->setText(Line->text()+"O");
194.}
195.void Mykeyboard::ClickP()
196.{
197. Line->setText(Line->text()+"P");
198.}
199.void Mykeyboard::ClickA()
200.{
201. Line->setText(Line->text()+"A");
202.}
203.void Mykeyboard::ClickS()
204.{
205. Line->setText(Line->text()+"S");
206.}
207.void Mykeyboard::ClickD()
208.{
209. Line->setText(Line->text()+"D");
210.}
211.void Mykeyboard::ClickF()
212.{
213. Line->setText(Line->text()+"F");
214.}
215.void Mykeyboard::ClickG()
216.{
217. Line->setText(Line->text()+"G");
218.}
219.void Mykeyboard::ClickH()
220.{
221. Line->setText(Line->text()+"H");
222.}
223.void Mykeyboard::ClickJ()
224.{
225. Line->setText(Line->text()+"J");
226.}
227.void Mykeyboard::ClickK()
228.{
229. Line->setText(Line->text()+"K");
230.}
231.void Mykeyboard::ClickL()
232.{
233. Line->setText(Line->text()+"L");
234.}
235.void Mykeyboard::ClickBack()
236.{
237. QString s = Line->text();
238. int len = s.length();
239. QString s2 = s.remove(len-1,1);
240. Line->setText(s2);
241.}
242.void Mykeyboard::ClickClose()
243.{
244. Line->setText("");
245. this->close();
246.}
247.void Mykeyboard::ClickZ()
248.{
249. Line->setText(Line->text()+"Z");
250.}
251.void Mykeyboard::ClickX()
252.{
253. Line->setText(Line->text()+"X");
254.}
255.void Mykeyboard::ClickC()
256.{
257. Line->setText(Line->text()+"C");
258.}
259.void Mykeyboard::ClickV()
260.{
261. Line->setText(Line->text()+"V");
262.}
263.void Mykeyboard::ClickB()
264.{
265. Line->setText(Line->text()+"B");
266.}
267.void Mykeyboard::ClickN()
268.{
269. Line->setText(Line->text()+"N");
270.}
271.void Mykeyboard::ClickM()
272.{
273. Line->setText(Line->text()+"M");
274.}
275.void Mykeyboard::ClickPoint()
276.{
277. Line->setText(Line->text()+".");
278.}
279.
280.void Mykeyboard::ClickEnter()
281.{
282. CurString = Line->text() ;
283. Line->setText("");
284. this->close();
285.}
286.
287.Mykeyboard::~Mykeyboard()
288.{
289. delete ui;
290. for(int i = 0 ; i < 40 ; ++i)
291. delete SoftKey[i] ;
292.
293. delete Line ;
}
通样继续在刚才的项目中添加新文件,选择Qt设计师界面类,界面模板选择Dialog without Buttons,类名用Result。
在result.h中加上友元friend class Widget。
在result.ui中拖入一个Label,去掉里面的text内容其他的设置参考前面的ui界面,如下图
在widget.h中添加
#include "mykeyboard.h"
#include "result.h"
成员添加
Mykeyboard *mkb ;
Result *curresult ;
进入widget.ui界面,右键Mykeyboard,转到槽,选择clicked(),在跳转的函数体中加入mkb->exec();右键Result,转到槽,选择clicked(),在跳转的函数体中加入 curresult->ui->label->setText(tr("The Result:%1").arg(mkb->CurString));
curresult->show();
至此,程序完成。运行程序,点击Mykeyboard,点击软键盘输入......
点击确定,回到主页后点击Result按钮,发现刚才的输入显示在窗口中