QtSDKとqrencodeを使ったQRコード画像生成
Revisão | eaa3528a40a5c350adc3a4bb66cac1b439c8dcd9 (tree) |
---|---|
Hora | 2012-10-22 22:56:17 |
Autor | arakaki <alucky4416@user...> |
Commiter | arakaki |
ADD: add Save QRCodeImage Function, add MenuBar.
@@ -1,6 +1,9 @@ | ||
1 | 1 | #include "mainwindow.h" |
2 | 2 | #include "ui_mainwindow.h" |
3 | 3 | |
4 | +#include <QFileDialog> | |
5 | +#include <QMessageBox> | |
6 | + | |
4 | 7 | #include <QDebug> |
5 | 8 | |
6 | 9 | MainWindow::MainWindow(QWidget *parent) : |
@@ -9,9 +12,11 @@ MainWindow::MainWindow(QWidget *parent) : | ||
9 | 12 | { |
10 | 13 | ui->setupUi(this); |
11 | 14 | |
15 | + image = NULL; | |
12 | 16 | ui->imgView->setScene(&scene); |
13 | 17 | |
14 | 18 | ui->lineEdit->setText(""); |
19 | + ui->lineEdit->setFocus(); | |
15 | 20 | ui->btnStart->setDisabled(true); |
16 | 21 | } |
17 | 22 |
@@ -61,14 +66,17 @@ QPixmap MainWindow::encodeTextToPixmap( QString text ) | ||
61 | 66 | #else |
62 | 67 | QByteArray a = text.toAscii(); |
63 | 68 | #endif |
64 | - QRcode* qrcode = encode( (unsigned char*)a.constData() ); | |
69 | + QRcode* qrcode = encode( (unsigned char*)a.constData() ); // Generate QRCode from string. | |
65 | 70 | if ( qrcode == NULL ) |
66 | 71 | { |
67 | 72 | return NULL; |
68 | 73 | } |
69 | 74 | |
70 | 75 | int realwidth = (qrcode->width + margin * 2) * size; |
71 | - QImage* image = new QImage( realwidth, realwidth, QImage::Format_Mono ); | |
76 | + if (image != NULL) { | |
77 | + delete(image); | |
78 | + } | |
79 | + image = new QImage( realwidth, realwidth, QImage::Format_Mono ); | |
72 | 80 | QPainter painter(image); |
73 | 81 | |
74 | 82 | painter.fillRect( QRect( 0, 0, realwidth, realwidth ), QColor( Qt::white ) ); |
@@ -100,7 +108,8 @@ QPixmap MainWindow::encodeTextToPixmap( QString text ) | ||
100 | 108 | painter.end(); |
101 | 109 | |
102 | 110 | QPixmap pixmap = QPixmap::fromImage(*image); |
103 | - delete(image); | |
111 | + | |
112 | + QRcode_free(qrcode); | |
104 | 113 | |
105 | 114 | return pixmap; |
106 | 115 | } |
@@ -122,3 +131,29 @@ void MainWindow::on_lineEdit_textChanged(const QString &arg1) | ||
122 | 131 | ui->btnStart->setDisabled(false); |
123 | 132 | } |
124 | 133 | } |
134 | + | |
135 | +void MainWindow::on_actionExit_triggered() | |
136 | +{ | |
137 | + close(); | |
138 | +} | |
139 | + | |
140 | +void MainWindow::on_actionSave_triggered() | |
141 | +{ | |
142 | + if (image == NULL || image->isNull()) { | |
143 | + ; // empty imagedata | |
144 | + } else { | |
145 | + QString fileName = QFileDialog::getSaveFileName( this, "Save Image", "./", "Image Files (*.png)" ); | |
146 | + if ( fileName.isEmpty() ) return; | |
147 | + image->save( fileName, "PNG"); | |
148 | + } | |
149 | +} | |
150 | + | |
151 | +void MainWindow::on_actionAbout_triggered() | |
152 | +{ | |
153 | + QMessageBox::about(this, "About", tr("QtQREncode v0.1, uses libqrencode library.")); | |
154 | +} | |
155 | + | |
156 | +void MainWindow::on_actionAbout_Qt_triggered() | |
157 | +{ | |
158 | + QApplication::aboutQt(); | |
159 | +} |
@@ -24,10 +24,18 @@ private slots: | ||
24 | 24 | |
25 | 25 | void on_lineEdit_textChanged(const QString &arg1); |
26 | 26 | |
27 | + void on_actionExit_triggered(); | |
28 | + | |
29 | + void on_actionSave_triggered(); | |
30 | + | |
31 | + void on_actionAbout_triggered(); | |
32 | + | |
33 | + void on_actionAbout_Qt_triggered(); | |
34 | + | |
27 | 35 | private: |
28 | 36 | Ui::MainWindow *ui; |
29 | 37 | |
30 | - QImage image; // 表示画像を保存しておく場所 | |
38 | + QImage *image; // 表示画像を保存しておく場所 | |
31 | 39 | QGraphicsScene scene; // QGraphicsView 上に表示させるための準備 |
32 | 40 | |
33 | 41 | QRcode *encode(const unsigned char *intext); |
@@ -11,14 +11,14 @@ | ||
11 | 11 | </rect> |
12 | 12 | </property> |
13 | 13 | <property name="windowTitle"> |
14 | - <string>MainWindow</string> | |
14 | + <string>QtQREncode</string> | |
15 | 15 | </property> |
16 | 16 | <widget class="QWidget" name="centralWidget"> |
17 | 17 | <widget class="QGraphicsView" name="imgView"> |
18 | 18 | <property name="geometry"> |
19 | 19 | <rect> |
20 | 20 | <x>50</x> |
21 | - <y>80</y> | |
21 | + <y>90</y> | |
22 | 22 | <width>371</width> |
23 | 23 | <height>331</height> |
24 | 24 | </rect> |
@@ -36,12 +36,15 @@ | ||
36 | 36 | <property name="text"> |
37 | 37 | <string>Input String</string> |
38 | 38 | </property> |
39 | + <property name="buddy"> | |
40 | + <cstring>lineEdit</cstring> | |
41 | + </property> | |
39 | 42 | </widget> |
40 | 43 | <widget class="QLabel" name="label_2"> |
41 | 44 | <property name="geometry"> |
42 | 45 | <rect> |
43 | 46 | <x>50</x> |
44 | - <y>60</y> | |
47 | + <y>70</y> | |
45 | 48 | <width>52</width> |
46 | 49 | <height>14</height> |
47 | 50 | </rect> |
@@ -49,8 +52,11 @@ | ||
49 | 52 | <property name="text"> |
50 | 53 | <string>QRcode</string> |
51 | 54 | </property> |
55 | + <property name="buddy"> | |
56 | + <cstring>imgView</cstring> | |
57 | + </property> | |
52 | 58 | </widget> |
53 | - <widget class="QWidget" name=""> | |
59 | + <widget class="QWidget" name="layoutWidget"> | |
54 | 60 | <property name="geometry"> |
55 | 61 | <rect> |
56 | 62 | <x>50</x> |
@@ -82,6 +88,23 @@ | ||
82 | 88 | <height>22</height> |
83 | 89 | </rect> |
84 | 90 | </property> |
91 | + <widget class="QMenu" name="menuFile"> | |
92 | + <property name="title"> | |
93 | + <string>File</string> | |
94 | + </property> | |
95 | + <addaction name="actionSave"/> | |
96 | + <addaction name="separator"/> | |
97 | + <addaction name="actionExit"/> | |
98 | + </widget> | |
99 | + <widget class="QMenu" name="menuHelp"> | |
100 | + <property name="title"> | |
101 | + <string>Help</string> | |
102 | + </property> | |
103 | + <addaction name="actionAbout"/> | |
104 | + <addaction name="actionAbout_Qt"/> | |
105 | + </widget> | |
106 | + <addaction name="menuFile"/> | |
107 | + <addaction name="menuHelp"/> | |
85 | 108 | </widget> |
86 | 109 | <widget class="QToolBar" name="mainToolBar"> |
87 | 110 | <attribute name="toolBarArea"> |
@@ -92,6 +115,32 @@ | ||
92 | 115 | </attribute> |
93 | 116 | </widget> |
94 | 117 | <widget class="QStatusBar" name="statusBar"/> |
118 | + <action name="actionSave"> | |
119 | + <property name="text"> | |
120 | + <string>Save</string> | |
121 | + </property> | |
122 | + <property name="shortcut"> | |
123 | + <string>Ctrl+S</string> | |
124 | + </property> | |
125 | + </action> | |
126 | + <action name="actionExit"> | |
127 | + <property name="text"> | |
128 | + <string>Exit</string> | |
129 | + </property> | |
130 | + <property name="shortcut"> | |
131 | + <string>Ctrl+Q</string> | |
132 | + </property> | |
133 | + </action> | |
134 | + <action name="actionAbout"> | |
135 | + <property name="text"> | |
136 | + <string>About</string> | |
137 | + </property> | |
138 | + </action> | |
139 | + <action name="actionAbout_Qt"> | |
140 | + <property name="text"> | |
141 | + <string>About Qt</string> | |
142 | + </property> | |
143 | + </action> | |
95 | 144 | </widget> |
96 | 145 | <layoutdefault spacing="6" margin="11"/> |
97 | 146 | <resources/> |