1. 程式人生 > 其它 >Qt中使用main函式中的引數

Qt中使用main函式中的引數

相關資料:

http://www.myexceptions.net/qt/295891.html

例項:

.pro

 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 typeobject.cpp 22 23 HEADERS += \ 24 mainwindow.h \
25 typeobject.h 26 27 FORMS += \ 28 mainwindow.ui 29 30 # Default rules for deployment. 31 qnx: target.path = /tmp/$${TARGET}/bin 32 else: unix:!android: target.path = /opt/$${TARGET}/bin 33 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 #include <QDebug>
 5 
 6 #include "typeobject.h"
 7 
 8 int main(int argc, char *argv[])
 9 {
10     qDebug() << "argc:" <<argc;
11     for(int i=0; i<argc; i++)
12         qDebug() << "argv:" << argv[i];
13     c_ss = argv[1];
14     qDebug() << "c_ss:" << c_ss;
15 
16     QApplication a(argc, argv);
17     MainWindow w;
18     w.show();
19     return a.exec();
20 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QDebug>
 6 
 7 
 8 
 9 QT_BEGIN_NAMESPACE
10 namespace Ui { class MainWindow; }
11 QT_END_NAMESPACE
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     MainWindow(QWidget *parent = nullptr);
19     ~MainWindow();
20 
21 private slots:
22     void on_pushButton_clicked();
23 
24 private:
25     Ui::MainWindow *ui;
26 };
27 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 #include "typeobject.h"
 5 
 6 MainWindow::MainWindow(QWidget *parent)
 7     : QMainWindow(parent)
 8     , ui(new Ui::MainWindow)
 9 {
10     ui->setupUi(this);
11     setWindowTitle("main argc argv");
12 }
13 
14 MainWindow::~MainWindow()
15 {
16     delete ui;
17 }
18 
19 
20 void MainWindow::on_pushButton_clicked()
21 {
22     qDebug() << "c_ss:" << c_ss;
23 }
View Code

typeobject.h

1 #ifndef TYPEOBJECT_H
2 #define TYPEOBJECT_H
3 
4 #include <QString>
5 
6 extern QString c_ss;
7 
8 #endif // TYPEOBJECT_H
View Code

typeobject.cpp

1 #include "typeobject.h"
2 
3 QString c_ss="";
View Code