gpt4 book ai didi

c++ - Qt + CMake + VC++ 生成命令提示符

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:07:35 25 4
gpt4 key购买 nike

我在尝试运行 qt 教程中的窗口布局示例时使用标题中提到的组合。主要看起来像这样:

#include <QtGui>

int main(int argc, char **argv) {
QApplication app(argc, argv);
QWidget window;
QLabel *label = new QLabel(QApplication::translate("windowlayout", "Name:"));
QLineEdit *lineEdit = new QLineEdit();

QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
layout->addWidget(lineEdit);
window.setLayout(layout);
window.setWindowTitle(
QApplication::translate("windowlayout", "Window layout"));
window.show();
return app.exec();
}

CMakeLists.txt 是这样的:

PROJECT(test)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)

FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR} ${QT_QTCORE_INCLUDE_DIR} ${QT_QTGUI_INCLUDE_DIR})

SET(test_SRCS main.cc)

QT4_AUTOMOC(${test_SRCS})

ADD_EXECUTABLE(test ${test_SRCS})

TARGET_LINK_LIBRARIES(test ${QT_QTGUI_LIBRARIES} ${QT_QTCORE_LIBRARIES})

构建和编译工作正常,但当我运行应用程序时,它总是显示命令提示符。我该如何避免这种情况?

最佳答案

您需要告诉 CMake 您想要一个 GUI 应用程序:

 # GUI Type
if(WIN32)
set(GUI_TYPE WIN32)
endif(WIN32)
if(APPLE)
set(GUI_TYPE MACOSX_BUNDLE)
endif(APPLE)

ADD_EXECUTABLE(test ${GUI_TYPE} ${test_SRCS})

请注意,当您在 Windows 上编译时,这会将程序入口从 main() 更改为 WinMain(),因此您需要将源代码修改为出色地。这是我通常做的:

#ifdef _WIN32
class Win32CommandLineConverter;

int CALLBACK WinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */)
{
Win32CommandLineConverter cmd_line;
return main(cmd_line.argc(), cmd_line.argv());
}


class Win32CommandLineConverter {
private:
std::unique_ptr<char*[]> argv_;
std::vector<std::unique_ptr<char[]>> storage_;
public:
Win32CommandLineConverter()
{
LPWSTR cmd_line = GetCommandLineW();
int argc;
LPWSTR* w_argv = CommandLineToArgvW(cmd_line, &argc);
argv_ = std::unique_ptr<char*[]>(new char*[argc]);
storage_.reserve(argc);
for(int i=0; i<argc; ++i) {
storage_.push_back(ConvertWArg(w_argv[i]));
argv_[i] = storage_.back().get();
}
LocalFree(w_argv);
}
int argc() const
{
return static_cast<int>(storage_.size());
}
char** argv() const
{
return argv_.get();
}
static std::unique_ptr<char[]> ConvertWArg(LPWSTR w_arg)
{
int size = WideCharToMultiByte(CP_UTF8, 0, w_arg, -1, nullptr, 0, nullptr, nullptr);
std::unique_ptr<char[]> ret(new char[size]);
WideCharToMultiByte(CP_UTF8, 0, w_arg, -1, ret.get(), size, nullptr, nullptr);
return ret;
}
};
#endif

关于c++ - Qt + CMake + VC++ 生成命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9555375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com