Qt-Qt之QVariant的使用分析_unix


代码实例:

.pro

Qt-Qt之QVariant的使用分析_#include_02

Qt-Qt之QVariant的使用分析_#define_03

1 QT       += core gui
2
3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4
5 CONFIG += c++11
6
7 # The following define makes your compiler emit warnings if you use
8 # any Qt feature that has been marked deprecated (the exact warnings
9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17
18 SOURCES += \
19 main.cpp \
20 mainwindow.cpp
21
22 HEADERS += \
23 mainwindow.h
24
25 FORMS += \
26 mainwindow.ui
27
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31

View Code

main.cpp

Qt-Qt之QVariant的使用分析_#include_02

Qt-Qt之QVariant的使用分析_#define_03

1 #include "mainwindow.h"
2
3 #include <QApplication>
4
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8 MainWindow w;
9 w.show();
10 return a.exec();
11

View Code

mainwindow.h

Qt-Qt之QVariant的使用分析_#include_02

Qt-Qt之QVariant的使用分析_#define_03

1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <QVariant>
6
7 QT_BEGIN_NAMESPACE
8 namespace Ui { class MainWindow; }
9 QT_END_NAMESPACE
10
11 class MainWindow : public QMainWindow
12 {
13 Q_OBJECT
14
15 public:
16 MainWindow(QWidget *parent = nullptr);
17 ~MainWindow();
18
19 private slots:
20 void on_pushButton_2_clicked();
21
22 void on_pushButton_clicked();
23
24 void on_pushButton_9_clicked();
25
26 void on_pushButton_10_clicked();
27
28 void on_pushButton_4_clicked();
29
30 void on_pushButton_3_clicked();
31
32 void on_pushButton_11_clicked();
33
34 void on_pushButton_6_clicked();
35
36 void on_pushButton_12_clicked();
37
38 void on_pushButton_5_clicked();
39
40 void on_pushButton_13_clicked();
41
42 void on_pushButton_14_clicked();
43
44 private:
45 Ui::MainWindow *ui;
46 QVariant m_pVariant;
47 };
48 #endif // MAINWINDOW_H

View Code

mainwindow.cpp

Qt-Qt之QVariant的使用分析_#include_02

Qt-Qt之QVariant的使用分析_#define_03

1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 MainWindow::MainWindow(QWidget *parent)
5 : QMainWindow(parent)
6 , ui(new Ui::MainWindow)
7 {
8 ui->setupUi(this);
9
10 setWindowTitle(QStringLiteral("Qt之QVariant的使用分析"));
11 }
12
13 MainWindow::~MainWindow()
14 {
15 delete ui;
16 }
17
18
19 void MainWindow::on_pushButton_2_clicked()
20 {
21 m_pVariant.setValue(5);
22 ui->textEdit->append(QString::number(m_pVariant.toInt()));
23 }
24
25 void MainWindow::on_pushButton_clicked()
26 {
27 m_pVariant.setValue(QString("aaa"));
28 ui->textEdit->append(m_pVariant.toString());
29 }
30
31 void MainWindow::on_pushButton_9_clicked()
32 {
33 m_pVariant.setValue(1.1);
34 ui->textEdit->append(QString::number(m_pVariant.toDouble()));
35 }
36
37 void MainWindow::on_pushButton_10_clicked()
38 {
39 ui->textEdit->append(QStringLiteral("太TM多了,你自己一个一个试吧!"));
40 }
41
42 void MainWindow::on_pushButton_4_clicked()
43 {
44 if (m_pVariant.isNull())
45 ui->textEdit->append("Null");
46 else
47 ui->textEdit->append("NoNull");
48 }
49
50 void MainWindow::on_pushButton_3_clicked()
51 {
52 m_pVariant.setValue(QString("12hf6"));
53 if (m_pVariant.convert(QMetaType::Int))
54 ui->textEdit->append("Int");
55 else
56 ui->textEdit->append("NoInt");
57 }
58
59 void MainWindow::on_pushButton_11_clicked()
60 {
61 m_pVariant.setValue(QString("12hf6"));
62 if (m_pVariant.canConvert(QMetaType::Int))
63 ui->textEdit->append("Int");
64 else
65 ui->textEdit->append("NoInt");
66 }
67
68 void MainWindow::on_pushButton_6_clicked()
69 {
70 m_pVariant.clear();
71 }
72
73 void MainWindow::on_pushButton_12_clicked()
74 {
75 if (m_pVariant.isValid())
76 ui->textEdit->append("true");
77 else
78 ui->textEdit->append("false");
79 }
80
81 void MainWindow::on_pushButton_5_clicked()
82 {
83 ui->textEdit->append(QString::number(m_pVariant.type()));
84 }
85
86 void MainWindow::on_pushButton_13_clicked()
87 {
88 ui->textEdit->append(m_pVariant.typeName());
89 }
90
91 void MainWindow::on_pushButton_14_clicked()
92 {
93 ui->textEdit->append(QString::number(m_pVariant.userType()));
94

View Code

mainwindow.ui

Qt-Qt之QVariant的使用分析_#include_02

Qt-Qt之QVariant的使用分析_#define_03

1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>MainWindow</class>
4 <widget class="QMainWindow" name="MainWindow">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>657</width>
10 <height>315</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>MainWindow</string>
15 </property>
16 <widget class="QWidget" name="centralwidget">
17 <layout class="QHBoxLayout" name="horizontalLayout">
18 <item>
19 <layout class="QVBoxLayout" name="verticalLayout">
20 <item>
21 <widget class="QTextEdit" name="textEdit"/>
22 </item>
23 <item>
24 <layout class="QHBoxLayout" name="horizontalLayout_2">
25 <item>
26 <widget class="QPushButton" name="pushButton_2">
27 <property name="text">
28 <string>增加元素Int</string>
29 </property>
30 </widget>
31 </item>
32 <item>
33 <widget class="QPushButton" name="pushButton">
34 <property name="text">
35 <string>增加元素QString</string>
36 </property>
37 </widget>
38 </item>
39 <item>
40 <widget class="QPushButton" name="pushButton_9">
41 <property name="text">
42 <string>增加元素double </string>
43 </property>
44 </widget>
45 </item>
46 <item>
47 <widget class="QPushButton" name="pushButton_10">
48 <property name="text">
49 <string>更多...</string>
50 </property>
51 </widget>
52 </item>
53 </layout>
54 </item>
55 <item>
56 <layout class="QHBoxLayout" name="horizontalLayout_3">
57 <item>
58 <widget class="QPushButton" name="pushButton_4">
59 <property name="text">
60 <string>判断空</string>
61 </property>
62 </widget>
63 </item>
64 <item>
65 <widget class="QPushButton" name="pushButton_12">
66 <property name="text">
67 <string>是否有效</string>
68 </property>
69 </widget>
70 </item>
71 <item>
72 <widget class="QPushButton" name="pushButton_3">
73 <property name="text">
74 <string>判断类型(严格)</string>
75 </property>
76 </widget>
77 </item>
78 <item>
79 <widget class="QPushButton" name="pushButton_11">
80 <property name="text">
81 <string>判断类型(宽松)</string>
82 </property>
83 </widget>
84 </item>
85 </layout>
86 </item>
87 <item>
88 <layout class="QHBoxLayout" name="horizontalLayout_4">
89 <item>
90 <widget class="QPushButton" name="pushButton_6">
91 <property name="text">
92 <string>清空</string>
93 </property>
94 </widget>
95 </item>
96 <item>
97 <widget class="QPushButton" name="pushButton_5">
98 <property name="text">
99 <string>返回类型</string>
100 </property>
101 </widget>
102 </item>
103 <item>
104 <widget class="QPushButton" name="pushButton_13">
105 <property name="text">
106 <string>返回类型名</string>
107 </property>
108 </widget>
109 </item>
110 <item>
111 <widget class="QPushButton" name="pushButton_14">
112 <property name="text">
113 <string>返回使用类型</string>
114 </property>
115 </widget>
116 </item>
117 </layout>
118 </item>
119 </layout>
120 </item>
121 </layout>
122 </widget>
123 </widget>
124 <resources/>
125 <connections/>
126

View Code

 

 

 

 

搜索

复制