English 中文(简体)
Asnchronously Ron Console Output and GUI in Qt
原标题:Asynchronously Run Console Output and GUI in Qt

我正在努力围绕一项青少年申请建立一个全球倡议。 我能够点击一个纽子,管理ole子,并在全球倡议本身展示ole果。 我如何能够做到这一点? 我在北草坪工作。

最佳回答

也可以尝试QProcess。 它为启动外部进程、阅读其I/O和在完成后等待或不等待提供Qt接口。

为了你的目的,它像你希望这一进程同步进行一样,这样,法典可能会像:

我的开工者:

#include <QObject>
#include <QProcess>
#include <QDebug>


class MyProcessStarter : public QObject
{
    Q_OBJECT
public:
    MyProcessStarter() : QObject() {};
    void StartProcess();
private slots:
    void readStandardOutput();
private:
    QProcess *myProcess;
};

主编:

#include "myprocessstarter.h"

void MyProcessStarter::StartProcess()
{
    QString program = "dir";
    QStringList arguments;
    // Add any arguments you want to be passed

    myProcess = new QProcess(this);
    connect(myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
    myProcess->start(program, arguments);
}

void MyProcessStarter::readStandardOutput()
{
    QByteArray processOutput;
    processOutput = myProcess->readAllStandardOutput();

    qDebug() << "Output was " << QString(processOutput);
}

void main(int argc, char** argv)
{
    MyProcessStarter s;
    s.StartProcess();
}
问题回答

我在一份申请中想做类似的事情。 我将所有产出从标准流程(排出)转向我的ole窗。 定期阅读上游内容 对我的工作罚款。

StdRedirector.cpp

#include "StdRedirector.h"

QMutex coutMutex;

void outcallback(const char* ptr, std::streamsize count, void* bufferString)
{
    string *b = (string *) bufferString;    
    string t;

    for (int i=0; i < count; i++)
    {
        if (ptr[i] ==  
 )
        {
            t = t + "
";
        } else {
            t = t + ptr[i];
        }
    }       

    coutMutex.lock();
    *b = *b + t;
    coutMutex.unlock();
}

void ConsoleWindow::updateTimer(void)
{   
    coutMutex.lock();
    if (bufferString.size() > 0)
    {
        consoleBox->insertPlainText(QString(bufferString.c_str()));
        bufferString.clear();

        QScrollBar *sb = consoleBox->verticalScrollBar();
        sb->setValue(sb->maximum());        
    }
    coutMutex.unlock();
}

ConsoleWindow::ConsoleWindow(QWidget *parent) : QWidget(parent)
{
    consoleBox = new QTextEdit(this);
    consoleBox->setReadOnly(true);

    stdRedirector = new StdRedirector<>(std::cout, outcallback, &bufferString);

    QVBoxLayout *vb = new QVBoxLayout();
    vb->addWidget(consoleBox);
    vb->setMargin(0);
    vb->setSpacing(0);  

    setLayout(vb);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
    timer->start(100);
}

ConsoleWindow::~ConsoleWindow()
{
    delete stdRedirector;
}

StdRedirector.h

#ifndef STD_REDIRECTOR
#define STD_REDIRECTOR

#include <QWidget>
#include <QTextEdit>
#include <QString>
#include <QVBoxLayout>
#include <QTimer.h>
#include <QMutex>
#include <QScrollBar>

#include <iostream>
#include <string>

using namespace std;

template<class Elem = char, class Tr = std::char_traits<Elem>>

class StdRedirector : public std::basic_streambuf<Elem, Tr>
{
    typedef void (*pfncb) ( const Elem*, std::streamsize _Count, void* pUsrData );

    public:
        StdRedirector(std::ostream& a_Stream, pfncb a_Cb, void* a_pUsrData) :
            m_Stream(a_Stream),
            m_pCbFunc(a_Cb),
            m_pUserData(a_pUsrData)
        {
            m_pBuf = m_Stream.rdbuf(this);
        }

        ~StdRedirector()
        {
            m_Stream.rdbuf(m_pBuf);
        }

        std::streamsize xsputn(const Elem* _Ptr, std::streamsize _Count)
        {
            m_pCbFunc(_Ptr, _Count, m_pUserData);
            return _Count;
        }

        typename Tr::int_type overflow(typename Tr::int_type v)
        {
            Elem ch = Tr::to_char_type(v);
            m_pCbFunc(&ch, 1, m_pUserData);
            return Tr::not_eof(v);
        }

    protected:
        std::basic_ostream<Elem, Tr>& m_Stream;
        std::streambuf* m_pBuf;
        pfncb m_pCbFunc;
        void* m_pUserData;
};

class ConsoleWindow : public QWidget
{
Q_OBJECT

    public:
        ConsoleWindow(QWidget *parent = 0);
        ~ConsoleWindow();

    public slots:
        void updateTimer(void);

    public:
        QTextEdit *consoleBox;
        StdRedirector<> *stdRedirector;
        string bufferString;
};

#endif

The StdRedirector category is based on code from this Forum post:

查阅popen(>功能,可以做你需要的事情。

然后,请通过<代码>FILE *至http://doc.qt.nokia.com/latest/qtextstream.html#QTextStream-3”rel=“nofollow”>QTextStream,并与它一道工作。

我建议,而不是显示德国马克的停产,它拥有独ole的产出,这基本上意味着你希望向用户传递的所有信息。

这样,你就能够从奥塞罗获得解答信息,并且能够从奥塞里获得这种信息,在连接方面造成潜在的错误,无论怎么可能发生,在《全球倡议》应用中完全控制了青.素的产出。 当然,这一产出也可以用于停产,因此可见于奥索尔,但也使你能够看到像WARNING LOG NOTICENO_THIS_WENT_WRONG这样的预设装置。 或你想向用户显示什么是你的切入。





相关问题
How to start to create an application GUI using C#?

HI! I am new to C# and plan to use it for my application GUI. I am trying to make my GUI similar to SPSS:http://www.spss.com/images/08/statistics_screens/ez_rfm-big.jpg Is this easy in C#? Is there ...

Automatic height of edit box

My shoes application has three items stacked on top of each other (with a stack, of course), in order: A banner An edit box Two buttons in a flow What I want to do is have the banner stay at it s ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

UI Convention: Shortcut key for application exit? [closed]

Is there a convention for the shortcut keys for application exit? Some applications uses Alt+X some others use Ctrl+ X and Ctrl+Q. Applications like FF and IE doesnot assign a shortcut at all. So is ...

热门标签