qt 5.x 学习笔记 (2)

先来放一波过程中用到的资料和官方文档好了。

basic layout_qt5.8

QBoxLayout Class_qt5.8

QString Class 5.8

QChar Class qt 5.8

Standard Dialogs Example qt 5.8

更新的部分还是放在最前面好了。。。

convert from QString to char *的时候有个坑。。。

In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation: See the following example for a demonstration:

举个栗子。。。。

1int main(int argc, char **argv)
2{
3 QApplication app(argc, argv);
4 QString str1 = "Test";
5 QByteArray ba = str1.toLatin1();
6 const char *c_str2 = ba.data(); 
7 printf("str2: %s", c_str2);
8 return app.exec();   
9}
**Note that it is necessary to store the bytearray before you call data() on it, a call like the following const char *c_str2 = str2.toLatin1().data(); will make the application crash as the QByteArray has not been stored and hence no longer exists.**

目前基本框架出来了。。。

可以打开一个文件。。加密。。保存。。。

加密算法部分暂时用倒序输出代替。。。

界面布局还没搞。。。一些提示之类的还没搞。。。。。。。。

接下来要做的。。。大概就是把des写进去了。。。

放一波代码好了。。。

 1/****************************************************************************
 2**
 3** Copyright (C) 2016 The Qt Company Ltd.
 4** Contact: https://www.qt.io/licensing/
 5**
 6** This file is part of the examples of the Qt Toolkit.
 7**
 8** $QT_BEGIN_LICENSE:BSD$
 9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef DIALOG_H
52#define DIALOG_H
53
54#include <QWidget>
55#include <QTextEdit>
56
57class QCheckBox;
58class QLabel;
59class QErrorMessage;
60
61class DialogOptionsWidget;
62
63class Dialog : public QWidget
64{
65    Q_OBJECT
66
67public:
68    Dialog(QWidget *parent = 0);
69
70private slots:
71
72    void setText();
 1    void setOpenFileName();
 2    void beginToCal();
 3    void setSaveFileName();
 4    void criticalMessage();
 5    void informationMessage();
 6    void questionMessage();
 7    void warningMessage();
 8    void errorMessage();
 9
10private:
11
12    QLabel *textLabel;
13
14
15    QLabel *openFileNameLabel;
16
17    QLabel *saveFileNameLabel;
18    QLabel *criticalLabel;
19    QLabel *informationLabel;
20    QLabel *questionLabel;
21    QLabel *warningLabel;
22    QLabel *errorLabel;
23    QErrorMessage *errorMessageDialog;
24    DialogOptionsWidget *fileDialogOptionsWidget;
25    DialogOptionsWidget *colorDialogOptionsWidget;
26    DialogOptionsWidget *fontDialogOptionsWidget;
27    QString openFilesPath;
28    QTextEdit *textEdit;
29    QString QTextSt;
30};
31
32#endif
 1/****************************************************************************
 2**
 3** Copyright (C) 2016 The Qt Company Ltd.
 4** Contact: https://www.qt.io/licensing/
 5**
 6** This file is part of the examples of the Qt Toolkit.
 7**
 8** $QT_BEGIN_LICENSE:BSD$
 9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52#include <QTextEdit>
53
54#include "dialog.h"
55
56#define MESSAGE \
57    Dialog::tr("<p>Message boxes have a caption, a text, " \
58               "and any number of buttons, each with standard or custom texts." \
59               "<p>Click a button to close the message box. Pressing the Esc button " \
60               "will activate the detected escape button (if any).")
61#define MESSAGE_DETAILS \
62    Dialog::tr("If a message box has detailed text, the user can reveal it " \
63               "by pressing the Show Details... button.")
64
65class DialogOptionsWidget : public QGroupBox
66{
67public:
68    explicit DialogOptionsWidget(QWidget *parent = 0);
1    void addCheckBox(const QString &text, int value);
2    void addSpacer();
3    int value() const;
4
5private:
6    typedef QPair<QCheckBox *, int> CheckBoxEntry;
7    QVBoxLayout *layout;
8    QList<CheckBoxEntry> checkBoxEntries;
9};
 1DialogOptionsWidget::DialogOptionsWidget(QWidget *parent) :
 2    QGroupBox(parent) , layout(new QVBoxLayout)
 3{
 4    //setTitle(Dialog::tr("Optionsssss"));
 5   // textEdit = new QTextEdit(this);
 6    //setCentralWidget(textEdit);
 7    //textEdit = new QTextEdit(this);
 8    //setCentralWidget(textEdit);
 9
10  //  setLayout(layout);
11}
  1void DialogOptionsWidget::addCheckBox(const QString &text, int value)
  2{
  3    QCheckBox *checkBox = new QCheckBox(text);
  4    layout->addWidget(checkBox);
  5    checkBoxEntries.append(CheckBoxEntry(checkBox, value));
  6}
  7
  8void DialogOptionsWidget::addSpacer()
  9{
 10    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
 11}
 12
 13int DialogOptionsWidget::value() const
 14{
 15    int result = 0;
 16    foreach (const CheckBoxEntry &checkboxEntry, checkBoxEntries)
 17        if (checkboxEntry.first->isChecked())
 18            result |= checkboxEntry.second;
 19    return result;
 20}
 21
 22Dialog::Dialog(QWidget *parent)
 23    : QWidget(parent)
 24{
 25    QVBoxLayout *verticalLayout;
 26    if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
 27        QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
 28        QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName(), this);
 29        horizontalLayout->addWidget(groupBox);
 30        verticalLayout = new QVBoxLayout(groupBox);
 31    } else {
 32        verticalLayout = new QVBoxLayout(this);
 33    }
 34
 35    QToolBox *toolbox = new QToolBox;
 36    verticalLayout->addWidget(toolbox);
 37
 38    errorMessageDialog = new QErrorMessage(this);
 39
 40    int frameStyle = QFrame::Sunken | QFrame::Panel;
 41
 42    textLabel = new QLabel;
 43    textLabel->setFrameStyle(frameStyle);
 44    QPushButton *textButton = new QPushButton(tr("加密文件"));
 45
 46    openFileNameLabel = new QLabel;
 47    openFileNameLabel->setFrameStyle(frameStyle);
 48    QPushButton *openFileNameButton =
 49            new QPushButton(tr("打开文件"));
 50
 51    QPushButton *calButton =
 52            new QPushButton(tr("加密"));
 53
 54
 55
 56    saveFileNameLabel = new QLabel;
 57    saveFileNameLabel->setFrameStyle(frameStyle);
 58    QPushButton *saveFileNameButton =
 59            new QPushButton(tr("QFileDialog::get&SaveFileName()"));
 60
 61    criticalLabel = new QLabel;
 62    criticalLabel->setFrameStyle(frameStyle);
 63    QPushButton *criticalButton =
 64            new QPushButton(tr("QMessageBox::critica&l()"));
 65
 66    informationLabel = new QLabel;
 67    informationLabel->setFrameStyle(frameStyle);
 68    QPushButton *informationButton =
 69            new QPushButton(tr("QMessageBox::i&nformation()"));
 70
 71    questionLabel = new QLabel;
 72    questionLabel->setFrameStyle(frameStyle);
 73    QPushButton *questionButton =
 74            new QPushButton(tr("QMessageBox::&question()"));
 75
 76    warningLabel = new QLabel;
 77    warningLabel->setFrameStyle(frameStyle);
 78    QPushButton *warningButton = new QPushButton(tr("QMessageBox::&warning()"));
 79
 80    errorLabel = new QLabel;
 81    errorLabel->setFrameStyle(frameStyle);
 82    QPushButton *errorButton =
 83            new QPushButton(tr("QErrorMessage::showM&essage()"));
 84
 85
 86    connect(textButton, &QAbstractButton::clicked, this, &Dialog::setText);
 87
 88    connect(openFileNameButton, &QAbstractButton::clicked,
 89            this, &Dialog::setOpenFileName);
 90
 91    connect(calButton,&QAbstractButton::clicked,
 92            this,&Dialog::beginToCal);
 93
 94    connect(saveFileNameButton, &QAbstractButton::clicked,
 95            this, &Dialog::setSaveFileName);
 96    connect(criticalButton, &QAbstractButton::clicked, this, &Dialog::criticalMessage);
 97    connect(informationButton, &QAbstractButton::clicked,
 98            this, &Dialog::informationMessage);
 99    connect(questionButton, &QAbstractButton::clicked, this, &Dialog::questionMessage);
100    connect(warningButton, &QAbstractButton::clicked, this, &Dialog::warningMessage);
101    connect(errorButton, &QAbstractButton::clicked, this, &Dialog::errorMessage);
102
103    QWidget *page = new QWidget;
104    QGridLayout *layout = new QGridLayout(page);
105    layout->setColumnStretch(1, 1);
106    layout->setColumnMinimumWidth(1, 250);
107
108    layout->addWidget(textButton, 3, 0);
109    layout->addWidget(textLabel, 3, 1);
110
111    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 5, 0);
112    toolbox->addItem(page, tr("Input Dialogs"));
113
114    const QString doNotUseNativeDialog = tr("Do not use native dialog");
115
116    page = new QWidget;
117    layout = new QGridLayout(page);
118    layout->setColumnStretch(1, 1);
119
120   /* colorDialogOptionsWidget = new DialogOptionsWidget;
121    colorDialogOptionsWidget->addCheckBox(doNotUseNativeDialog, QColorDialog::DontUseNativeDialog);
122    colorDialogOptionsWidget->addCheckBox(tr("Show alpha channel") , QColorDialog::ShowAlphaChannel);
123    colorDialogOptionsWidget->addCheckBox(tr("No buttons") , QColorDialog::NoButtons);
124    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 1, 0);
125    layout->addWidget(colorDialogOptionsWidget, 2, 0, 1 ,2);
126
127    toolbox->addItem(page, tr("Color Dialog"));
128    */
129
130   // page = new QWidget;
131   // layout = new QGridLayout(page);
132   // layout->setColumnStretch(1, 1);
133   // layout->addWidget(fontButton, 0, 0);
134    //layout->addWidget(fontLabel, 0, 1);
135
136
137    page = new QWidget;
138    layout = new QGridLayout(page);
139    layout->setColumnStretch(3,1);
140
141    layout->addWidget(openFileNameButton, 1, 0);
142    layout->addWidget(openFileNameLabel, 1,1);
143    layout->addWidget(calButton,1,2);
144
145    layout->addWidget(saveFileNameButton, 3, 0);
146    layout->addWidget(saveFileNameLabel, 3, 1);
147    fileDialogOptionsWidget = new DialogOptionsWidget;
148
149
150    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 4, 0);
151    toolbox->addItem(page, tr("File Dialogs"));
152
153
154    page = new QWidget;
155    layout = new QGridLayout(page);
156    layout->setColumnStretch(1, 1);
157    layout->addWidget(criticalButton, 0, 0);
158    layout->addWidget(criticalLabel, 0, 1);
159    layout->addWidget(informationButton, 1, 0);
160    layout->addWidget(informationLabel, 1, 1);
161    layout->addWidget(questionButton, 2, 0);
162    layout->addWidget(questionLabel, 2, 1);
163    layout->addWidget(warningButton, 3, 0);
164    layout->addWidget(warningLabel, 3, 1);
165    layout->addWidget(errorButton, 4, 0);
166    layout->addWidget(errorLabel, 4, 1);
167    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 5, 0);
168
169    toolbox->addItem(page, tr("Message Boxes"));
170
171    setWindowTitle(QGuiApplication::applicationDisplayName());
172}
173
174
175
176
177
178void Dialog::setText()
179{
180    bool ok;
181    QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
182                                         tr("User name:"), QLineEdit::Normal,
183                                         QDir::home().dirName(), &ok);
184    if (ok && !text.isEmpty())
185        textLabel->setText(text);
186}
187
188
189
190
191
192
193
194
195void Dialog::setOpenFileName()
196{
197    const QFileDialog::Options options = QFlag(fileDialogOptionsWidget->value());
198    QString selectedFilter;
199    QString fileName = QFileDialog::getOpenFileName(this,
200                                tr("QFileDialog::getOpenFileName()"),
201                                openFileNameLabel->text(),
202                                tr("All Files (*);;Text Files (*.txt)"),
203                                &selectedFilter,
204                                options);
205    if (!fileName.isEmpty())
206    {
207
208        openFileNameLabel->setText(fileName);
209        qDebug()<<"filename:"<<fileName;
210        QFile file(fileName);
211
212        //不仅仅是if判断。。重点是执行了open操作。。。顺便返回值罢了。。kk最菜.jpg
213
214        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
215            QMessageBox::warning(this, tr("Read File"),
216                                 tr("Cannot open file:\n%1").arg(fileName));
217            return;
218        }
219
220        QTextStream in(&file);
221        QTextSt = in.readAll();
222        qDebug()<<"fileText:"<<QTextSt<<endl;
223        qDebug()<<"TextLen:"<<QTextSt.length();
224          file.close();
225     }
226}
227
228
229
230void Dialog::beginToCal()
231{
232    int len = QTextSt.length();
233    QString ret = "";
234    for ( int i = len-1 ; i >=0 ; i--)
235    {
236           QChar tmp = QTextSt[i];
237           ret+=tmp;
238
239    }
240    QTextSt = ret;
241}
242
243void Dialog::setSaveFileName()
244{
245    const QFileDialog::Options options = QFlag(fileDialogOptionsWidget->value());
246    QString selectedFilter;
247    QString fileName = QFileDialog::getSaveFileName(this,
248                                tr("QFileDialog::getSaveFileName()"),
249                                saveFileNameLabel->text(),
250                                tr("All Files (*);;Text Files (*.txt)"),
251                                &selectedFilter,
252                                options);
253
254    if (!fileName.isEmpty())
255    {
256        saveFileNameLabel->setText(fileName);
257        QFile file(fileName);
258        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
259            QMessageBox::warning(this, tr("Write File"),
260                                       tr("Cannot open file:\n%1").arg(fileName));
261            return;
262        }
263        QTextStream out(&file);
264        qDebug()<<"save file text:"<<QTextSt<<endl;
265        //out << textEdit->toPlainText();
266        out<<QTextSt<<endl;
267        file.close();
268     }
269
270
271}
272
273void Dialog::criticalMessage()
274{
275    QMessageBox::StandardButton reply;
276    reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
277                                    MESSAGE,
278                                    QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore);
279    if (reply == QMessageBox::Abort)
280        criticalLabel->setText(tr("Abort"));
281    else if (reply == QMessageBox::Retry)
282        criticalLabel->setText(tr("Retry"));
283    else
284        criticalLabel->setText(tr("Ignore"));
285}
286
287void Dialog::informationMessage()
288{
289    QMessageBox::StandardButton reply;
290    reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);
291    if (reply == QMessageBox::Ok)
292        informationLabel->setText(tr("OK"));
293    else
294        informationLabel->setText(tr("Escape"));
295}
296
297void Dialog::questionMessage()
298{
299    QMessageBox::StandardButton reply;
300    reply = QMessageBox::question(this, tr("QMessageBox::question()"),
301                                    MESSAGE,
302                                    QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
303    if (reply == QMessageBox::Yes)
304        questionLabel->setText(tr("Yes"));
305    else if (reply == QMessageBox::No)
306        questionLabel->setText(tr("No"));
307    else
308        questionLabel->setText(tr("Cancel"));
309}
310
311void Dialog::warningMessage()
312{
313    QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),
314                       MESSAGE, 0, this);
315    msgBox.setDetailedText(MESSAGE_DETAILS);
316    msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole);
317    msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole);
318    if (msgBox.exec() == QMessageBox::AcceptRole)
319        warningLabel->setText(tr("Save Again"));
320    else
321        warningLabel->setText(tr("Continue"));
322
323}
324
325void Dialog::errorMessage()
326{
327    errorMessageDialog->showMessage(
328            tr("This dialog shows and remembers error messages. "
329               "If the checkbox is checked (as it is by default), "
330               "the shown message will be shown again, "
331               "but if the user unchecks the box the message "
332               "will not appear again if QErrorMessage::showMessage() "
333               "is called with the same message."));
334    errorLabel->setText(tr("If the box is unchecked, the message "
335                           "won't appear again."));
336}
 1/****************************************************************************
 2**
 3** Copyright (C) 2016 The Qt Company Ltd.
 4** Contact: https://www.qt.io/licensing/
 5**
 6** This file is part of the examples of the Qt Toolkit.
 7**
 8** $QT_BEGIN_LICENSE:BSD$
 9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
 1#include <QApplication>
 2#include <QStyleHints>
 3#include <QDesktopWidget>
 4#include <QTranslator>
 5#include <QLocale>
 6#include <QLibraryInfo>
 7
 8#include "dialog.h"
 9
10int main(int argc, char *argv[])
11{
12    QApplication app(argc, argv);
13    QGuiApplication::setApplicationDisplayName(Dialog::tr("Standard Dialogs"));
14
15#ifndef QT_NO_TRANSLATION
16    QString translatorFileName = QLatin1String("qt_");
17    translatorFileName += QLocale::system().name();
18    QTranslator *translator = new QTranslator(&app);
19    if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
20        app.installTranslator(translator);
21#endif
22
23    Dialog dialog;
24    if (!QGuiApplication::styleHints()->showIsFullScreen() && !QGuiApplication::styleHints()->showIsMaximized()) {
25        const QRect availableGeometry = QApplication::desktop()->availableGeometry(&dialog);
26        dialog.resize(availableGeometry.width() * 4 / 5, availableGeometry.height() * 2 / 3);
27        dialog.move((availableGeometry.width() - dialog.width()) / 2,
28                    (availableGeometry.height() - dialog.height()) / 2);
29    }
30    dialog.show();
31
32    return app.exec();
33}

记录一些坑?(其实也不算坑。。。

目前我的做法是有一个全局的QString。。。

我是打算直接拆分包装这个QString。。。每8位。。。

然后QString的[] operator得到的是QCharRef类型。。。。

QString之前有的toAscii()被移除了。。。。

qt 5.2以后的方法叫:toLatin1()

qt5.8_doc_toLatin1()