Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用qt.widgetapp 无法向控制台输出文字内容 #3371

Closed
lolikonloli opened this issue Feb 16, 2023 · 2 comments
Closed

使用qt.widgetapp 无法向控制台输出文字内容 #3371

lolikonloli opened this issue Feb 16, 2023 · 2 comments
Labels

Comments

@lolikonloli
Copy link

Xmake 版本

v2.7.5+dev.3508a43f9

操作系统版本和架构

windows11 22H2; wsl ubuntu20.04

描述问题

使用qt.widgetapp模板后, 无法再控制台中输出任何信息
但是相同的代码可以在ubuntu20.04的wsl中, 可以正常向控制台输出

代码如下:

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <string>
#include <iostream>
#include <stdio.h>

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    cv::Mat image;
    
    qDebug() << "qdebug, hellp qt5";
    std::cout << "std, hellp qt5\n";
    printf("print, hello qt5\n");

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

在win11中, 终端并没有任何文字输出, 如下图所示:
image

在wsl中, 终端会输出这三段文字:
image

期待的结果

希望在win中, 通过qDebug()也可以正常输出文字

工程配置

xmake.lua内容如下:

add_rules("mode.debug", "mode.release")

add_requires("opencv", "qt5base")

target("xmake_qt5_test")
    add_packages("qt5base", "opencv")

    add_rules("qt.widgetapp")
    -- add_rules("qt.console")

    add_headerfiles("src/*.h")
    add_files("src/*.cpp")
    add_files("src/mainwindow.ui")
    -- add files with Q_OBJECT meta (only for qt.moc)
    add_files("src/mainwindow.h")

附加信息和错误日志

No response

@waruqi
Copy link
Member

waruqi commented Feb 16, 2023

这个跟 xmake 没啥关系,自己代码问题,widgetapp 属于 gui 程序,在 windows 上就不走 console subsystem 了,它跟 linux 原本就是不一样的。。

已经切到 windows subsystem,还要在显示 gui 的同时,同时回显输出终端,得写代码专门设置,自己 google ,网上相关文档很多的。。属于 windows 程序开发的问题,跟 xmake 没任何关系

@waruqi waruqi closed this as completed Feb 16, 2023
@Heafei
Copy link

Heafei commented Jan 26, 2024

这个我会,参考如下代码,重定向标准输出

#ifdef _WIN32
#include "Windows.h"
#endif

main 函数中将标准输出和标准错误输出重定向到 Windows 控制台

    // 将标准输出和标准错误输出重定向到 Windows 控制台
#ifdef _WIN32
    if (AttachConsole(ATTACH_PARENT_PROCESS))
    {
        freopen("CONOUT$", "w", stdout);
        freopen("CONOUT$", "w", stderr);
    }
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants