zoukankan      html  css  js  c++  java
  • Qt中使用main函数中的参数

    相关资料:

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

    https://download.csdn.net/download/zhujianqiangqq/19569564    代码包下载

    实例:

    .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     QString sName = QString(c_ss);
    17     qDebug() << "sName:" << sName;
    18     qDebug() << "sName:" << sName;
    19 
    20     QApplication a(argc, argv);
    21     MainWindow w;
    22     w.show();
    23     return a.exec();
    24 }
    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

     PS:参数用的是Qtcreator调时传入的。方法:“项目”-“Run”-"Command line arguments:"-输入"ASD"。

    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    BZOJ5296 [CQOI2018] 破解D-H协议 【数学】【BSGS】
    Codeforces963C Frequency of String 【字符串】【AC自动机】
    Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】
    Hello World
    Codeforces963C Cutting Rectangle 【数学】
    BZOJ5203 [NEERC2017 Northern] Grand Test 【dfs树】【构造】
    20160422 --Switch…case 总结; 递归算法
    20160421字符串类型;日期时间类型数学类型
    20160420冒泡排序和查找
    20160419 while练习,复习
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14875188.html
Copyright © 2011-2022 走看看