如何从我的代码中确定我的应用程序是从Qt Creator启动的(通过“F5”还是“Ctrl + R”)?
我想知道这个的原因是因为我想直接从可执行文件启动应用程序时创建自己的调试消息处理程序(使用qInstallMessageHandler()) 。由于Qt只允许一个消息处理程序,我不想在从Qt Creator启动时创建自己的消息,否则我在Qt Creators自己的调试控制台中看不到调试消息。
答案 0 :(得分:3)
我不认为有一种简单的方法可以检测到这一点。 您可以在QtCreator中为运行设置添加命令行参数,并在运行时检查它。
答案 1 :(得分:2)
我有两种可能的解决方案:
检查父进程(或父进程的父进程)的名称
有多种方法可以做到这一点:在Posix(mingw,linux等等)下你有getppid()。在Windows下,您可以检查名称是Psapi还是其他进程处理函数。我过去曾为其他目的这样做,只要进程名称不变,它就可以正常工作。或者,您可以检查窗口名称。不幸的是,这些解决方案都不是“Qt-Native”。
仅在从Qt creator
启动时提供命令行参数如果您使用库来扫描commanline参数,这可能是最简单的解决方案。我通常使用Boost Program Options(谷歌那里)。您可以创建一个命令行参数,例如“--console-log”,它指定将日志输出放到控制台。要在Qt Creator中设置该选项,请记录here.这可能是最“Qt-Native”解决方案。如果使用Qt函数解析命令行,则为100%Qt。
答案 2 :(得分:1)
您可以尝试使用IsDebuggerPresent或仅使用QDebug,并在QT Creator外部使用debugview检查调试消息。
答案 3 :(得分:0)
您应该google qInstallMessageHandler了解更多信息。
但是这里有定义:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include <QtWidgets>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
// If you're running lots of threads you'll need to do some research to
// determine if you need to make this synchronized (i.e. locked)
if(staticTextEdit) {
staticTextEdit->appendPlainText(msg + "\n");
}
}
private slots:
void on_pushButton_clicked();
private:
// There's lots of different ways to do this ... this was the quickest way for me.
static QPlainTextEdit* staticTextEdit;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
以下是声明:
#include "mainwindow.h"
#include "ui_mainwindow.h"
QPlainTextEdit* MainWindow::staticTextEdit = NULL;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
staticTextEdit = ui->plainTextEdit;
qInstallMessageHandler(MainWindow::myMessageHandler);
}
MainWindow::~MainWindow()
{
delete ui;
}
// I'm using QtCreator to help create my slots. =).
void MainWindow::on_pushButton_clicked()
{
qDebug() << "CLICKED";
}
答案 4 :(得分:0)
这是一个手动解决方案。
在.pro文件中定义一个宏....
# To turn on remove the comment marker '#'
# isEmpty(LAUNCH_FROM_IDE):LAUNCH_FROM_IDE = 1
!isEmpty(LAUNCH_FROM_IDE) {
DEFINES += APP_LAUNCH_FROM_IDE
}
根据需要在标题/来源中使用...
#ifdef APP_LAUNCH_FROM_IDE
...
#endif
全部
答案 5 :(得分:0)
不确定这是否适用于PC,但在OSX下,当Qt Creator启动应用程序时,它是从开发构建路径执行的;您可以通过以下方式获取当前的启动路径:
QString expath = QCoreApplication::applicationDirPath();
得到它,通过qDebug()
转储它,并从Qt启动时查看运行的位置。只要这不是您不在Qt中启动应用程序时从那里运行应用程序的位置,就应该可以。
在Qt Creator中以发布模式构建和运行时,我是从qDebug()获得此信息的:
"/Users/fyngyrz/build-iToolBox-Desktop_Qt_5_8_0_clang_64bit-Release/iToolBox.app/Contents/MacOS/iToolBox"
所以对我来说,在构建应用iToolBox
时,我会像这样进行检测:
if (expath.indexOf("build-iToolBox-Desktop") != -1) // if we're running from inside Qt
{
}
else // we're not running from inside qt
{
}
默认情况下,Qt构建的发布和构建路径是不同的;因此,如果您需要在发布和调试环境中都可以使用它,则可能必须对路径进行两次检查而不是一次检查,具体取决于您要搜索的内容。
如果将这种方法合并到您的应用程序中,并且代码在发行版本中将保持不变,请确保不要在搜索字符串中包含任何会包含您不想共享的信息(例如您的真实信息)的内容在某些操作系统下,该名称可能是路径的一部分,具体取决于您如何设置用户帐户。