I'm using QFileDialog to select a directory. I'm having an issue that I'm unable to resolve. I've spent a lot of time googling for this but have come up with zilch.
I specify the starting directory (say /home/dhoti/downloads) and I want to disable navigation above this directory. For example the user should not be allowed to go to /home/dhoti or /tmp etc. How do I achieve this?
Here is my code:
QFileDialog dlg(this, "Select Firmware Version");
dlg.setDirectory("/home/dhoti/downloads");
dlg.setFileMode(QFileDialog::DirectoryOnly);
dlg.setOption(QFileDialog::ReadOnly, true);
dlg.setOption(QFileDialog::HideNameFilterDetails, true);
dlg.setViewMode(QFileDialog::List);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.exec();
qDebug() << "selected files: " << dlg.selectedFiles();
thanks for any help
Dhoti
You can detect when the current directory changes and if it is beyond your limit, set the directory back to the limit directory.
You can do this by executing the dialog non-blocking, and connecting the QFileDialog::directoryEntered(const QString& directory) signal to a slot of your own where you can do the checking. If it fails your check, set the current directory to the limit directory by QFileDialog::setDirectory(const QString& directory).
Disclaimer I have not tried this, but I'll be surprised if it does not work.
Try the following:
filedialog.h
#ifndef FILEDIALOG_H
#define FILEDIALOG_H
class QEvent;
#include <QFileDialog>
#include <QString>
class FileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit FileDialog(QWidget *parent = 0);
public:
bool eventFilter(QObject *o, QEvent *e);
void setTopDir(const QString &path);
QString topDir() const;
private:
bool pathFits(const QString &path) const;
private slots:
void checkHistory();
void checkGoToParent();
void checkLineEdit(const QString &text);
private:
QString mtopDir;
};
#endif // FILEDIALOG_H
filedialog.cpp
#include "filedialog.h"
#include <QString>
#include <QStringList>
#include <QFileDialog>
#include <QList>
#include <QToolButton>
#include <QDir>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QEvent>
#include <QKeyEvent>
#include <QAbstractButton>
#include <QCompleter>
#include <QAbstractItemView>
#include <QFileInfo>
FileDialog::FileDialog(QWidget *parent) :
QFileDialog(parent)
{
connect(this, SIGNAL(directoryEntered(QString)), this, SLOT(checkHistory()));
connect(this, SIGNAL(directoryEntered(QString)), this, SLOT(checkGoToParent()));
connect(findChild<QToolButton *>("backButton"), SIGNAL(clicked()), this, SLOT(checkGoToParent()));
connect(findChild<QToolButton *>("forwardButton"), SIGNAL(clicked()), this, SLOT(checkGoToParent()));
connect(findChild<QLineEdit *>("fileNameEdit"), SIGNAL(textChanged(QString)), this, SLOT(checkLineEdit(QString)));
findChild<QLineEdit *>("fileNameEdit")->installEventFilter(this);
findChild<QWidget *>("listView")->installEventFilter(this);
findChild<QWidget *>("treeView")->installEventFilter(this);
findChild<QLineEdit *>("fileNameEdit")->completer()->popup()->installEventFilter(this);
setOption(DontUseNativeDialog, true);
}
bool FileDialog::eventFilter(QObject *o, QEvent *e)
{
if (e->type() != QEvent::KeyPress)
return false;
int key = static_cast<QKeyEvent *>(e)->key();
if (o->objectName() == "listView" || o->objectName() == "treeView")
{
return (Qt::Key_Backspace == key && !pathFits(directory().absolutePath()));
}
else
{
if (Qt::Key_Return != key && Qt::Key_Enter != key)
return false;
QString text = findChild<QLineEdit *>("fileNameEdit")->text();
QString path = QDir::cleanPath(directory().absolutePath() + (text.startsWith("/") ? "" : "/") + text);
bool a = QDir(text).isAbsolute();
return !((!a && pathFits(path)) || (a && pathFits(text)));
}
}
void FileDialog::setTopDir(const QString &path)
{
if (path == mtopDir)
return;
mtopDir = (!path.isEmpty() && QFileInfo(path).isDir()) ? path : QString();
if (!pathFits(path))
{
setDirectory(mtopDir);
checkHistory();
checkLineEdit(findChild<QLineEdit *>("fileNameEdit")->text());
}
else
{
QLineEdit *ledt = findChild<QLineEdit *>("fileNameEdit");
ledt->setText(ledt->text());
}
findChild<QWidget *>("lookInCombo")->setEnabled(mtopDir.isEmpty());
findChild<QWidget *>("sidebar")->setEnabled(mtopDir.isEmpty());
checkGoToParent();
}
QString FileDialog::topDir() const
{
return mtopDir;
}
bool FileDialog::pathFits(const QString &path) const
{
return mtopDir.isEmpty() || (path.startsWith(mtopDir) && path.length() > mtopDir.length());
}
void FileDialog::checkHistory()
{
QStringList list = history();
for (int i = list.size() - 1; i >= 0; --i)
if (!pathFits(list.at(i)))
list.removeAt(i);
setHistory(list);
}
void FileDialog::checkGoToParent()
{
findChild<QToolButton *>("toParentButton")->setEnabled(pathFits(directory().absolutePath()));
}
void FileDialog::checkLineEdit(const QString &text)
{
QAbstractButton *btn = findChild<QDialogButtonBox *>("buttonBox")->buttons().first();
QString path = QDir::cleanPath(directory().absolutePath() + (text.startsWith("/") ? "" : "/") + text);
bool a = QDir(text).isAbsolute();
btn->setEnabled(btn->isEnabled() && ((!a && pathFits(path)) || (a && pathFits(text))));
}
This code may look like some magic, and it's not perfect, but it works. I searched for QFileDialog child objects names in Qt sources and used
findChild()
method to access them. All you need is just use the
setTopDir()
method to specify a directory above which users are not allowed to go.
Here's an example project using this class: https://docs.google.com/file/d/0B3P3dwuDIZ1-Q19FbkFMY2puUE0/edit?usp=sharing
You can use the solution of #ololoepepe. And clean unwanted entries in the comboBox on the top with this:
connect(findChild<QComboBox *>("lookInCombo"), static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileDialog::checkComboBox);
void FileDialog::checkComboBox(int index) {
int i;
QComboBox *cb = findChild<QComboBox *>("lookInCombo");
if (index == 0 && cb->model()->rowCount() > 1) {
for (i = 0; i < cb->model()->rowCount(); ++i) {
if (!pathFits(cb->model()->index(i, 0).data().toString() + "/")) {
cb->model()->removeRow(i);
--i;
}
}
}
}
Here is the simplest solution, with the minimum steps required to limit a directory traversal.
Idea: use public signal directoryEntered(const QString &) of QFileDialog to get notification when directory might be changed, implement slot for it in one of your classes and place there a logic for making sure that directory is the one you need.
QFileDialog dialog(this);
connect(&dialog, SIGNAL(directoryEntered(const QString &)), this, SLOT(onFileDialogDirectoryChanged(const QString &)));
Related
I have read the QIODevice doc, but still don't know how to archive that.
What I want to do is to create a KeyBoard class deriving from QIODevice. which opens /dev/input/eventX. I hope that my code can use the readyRead() signal of KeyBoard. (QFile does not emit readyRead() signal)
class KeyBoard : public QIODevice {
public:
KeyBoard();
~KeyBoard();
protected:
qint64 readData(char *data, qint64 size);
qint64 writeData(const char *data, qint64 size);
};
What do I need to do in readData() and writeData()?
And how does my code use this class? (I just use the QCoreApplication, no gui)
Use QSocketNotifier on the open file handle. You can read from the device using QFile,or abuse QSerialPort, i.e. QSerialPort m_port{"input/eventX"}. See this answer for an example of using QSocketNotifier with stdin; /dev/input/eventX requires a similar approach.
Here's an example that works on /dev/stdio, but would work identically on /dev/input/eventX.
// https://github.com/KubaO/stackoverflown/tree/master/questions/dev-notifier-49402735
#include <QtCore>
#include <fcntl.h>
#include <boost/optional.hpp>
class DeviceFile : public QFile {
Q_OBJECT
boost::optional<QSocketNotifier> m_notifier;
public:
DeviceFile() {}
DeviceFile(const QString &name) : QFile(name) {}
DeviceFile(QObject * parent = {}) : QFile(parent) {}
DeviceFile(const QString &name, QObject *parent) : QFile(name, parent) {}
bool open(OpenMode flags) override {
return
QFile::isOpen()
|| QFile::open(flags)
&& fcntl(handle(), F_SETFL, O_NONBLOCK) != -1
&& (m_notifier.emplace(this->handle(), QSocketNotifier::Read, this), true)
&& m_notifier->isEnabled()
&& connect(&*m_notifier, &QSocketNotifier::activated, this, &QIODevice::readyRead)
|| (close(), false);
}
void close() override {
m_notifier.reset();
QFile::close();
}
};
int main(int argc, char **argv) {
QCoreApplication app{argc, argv};
DeviceFile dev("/dev/stdin");
QObject::connect(&dev, &QIODevice::readyRead, [&]{
qDebug() << "*";
if (dev.readAll().contains('q'))
app.quit();
});
if (dev.open(QIODevice::ReadOnly))
return app.exec();
}
#include "main.moc"
I know how to apply a keyboard shortcut to an action. And in some software such as Visual Studio there are shortcuts that do the job in more than one step (such as Ctrl+K,Ctrl+C to comment the code).
Another example of that in Sublime Text:
I wonder whether or not it is possible to implement in Qt.
You can create it by using the multiple arguments constructor for QKeySequence.
like this:
auto ac = new QAction(this);
ac->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_K, Qt::CTRL + Qt::Key_C));
Try this:
action->setShortcut("Ctrl+K,Ctrl+C");
QKeySequence may be implicitly created from QString.
Due to documentation:
Up to four key codes may be entered by separating them with commas, e.g. "Alt+X,Ctrl+S,Q".
MOC generates almost same code when you create shortcut for a QAction via Qt Designer. But it makes it slightly different:
action->setShortcut(QApplication::translate("MainWindow", "Ctrl+K, Ctrl+C", 0));
but it's actually same thing.
You can use eventFilter to get mouse & keyboard events.
I use boolean to get first and second key, Ctrl + K then C.
I made you a sample code it's working.
.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
firstKey = false;
secondKey = false;
this->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == this &&event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ((keyEvent->key() == Qt::Key_Control))
{
firstKey = true;
return true;
}
else if ((keyEvent->key() == Qt::Key_K))
{
secondKey = true;
return true;
}
else if ((keyEvent->key() == Qt::Key_C))
{
if(firstKey && secondKey)
{
firstKey = false;
secondKey = false;
QMessageBox::information(this, "", "Ctrl + k + c");
}
return true;
}
else
return false;
}
else
return false;
}
void MainWindow::keyReleaseEvent(QKeyEvent *e)
{
if (e->type() == QEvent::KeyRelease)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
if ((keyEvent->key() == Qt::Key_Control))
{
firstKey = false;
}
}
}
.h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QMessageBox>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool firstKey;
bool secondKey;
bool eventFilter(QObject *object, QEvent *event);
void keyReleaseEvent(QKeyEvent *e);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
I want to draw a line using QGraphicsLineItem. What exactly I want is that on clicking at GraphicsView, after second click Line must be drawn. I am confused with the syntax of QGraphicsLineItem and also how to use it. I am new to Qt. Please help me out to solve this problem.
You can use this code snippet.
*h
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QStack>
#include <QPoint>
#include <QMouseEvent>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
public slots:
private:
QStack<QPoint> stack;
};
#endif // GRAPHICSSCENE_H
*.cpp
#include "graphicsscene.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
qDebug() << "in";
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint pos = mouseEvent->scenePos().toPoint();
if(stack.isEmpty())
stack.append(pos);
else if(stack.count() == 1)
{
stack.append(pos);
addLine(QLine(stack.pop(),stack.pop()),QPen(Qt::green));
}
}
}
Usage:
GraphicsScene *scene = new GraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
Edit: more beautiful solution which works as you need.
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
qDebug() << "in";
if (mouseEvent->button() == Qt::LeftButton)
{
QPoint pos = mouseEvent->scenePos().toPoint();
if(stack.isEmpty())
stack.append(pos);
else
addLine(QLine(pos,stack.pop()),QPen(Qt::green));
}
}
You can derive the graphics view/scene and override the mousePressEvent
Below is example using derived QGraphicsScene and overridden mousePressEvent
Class Definition :
class MyScene : public QGraphicsScene
Data Members :
QList<QPointF> m_clickPositions;
int m_mode;
Code :
void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(false == sceneRect().contains(event->scenePos()))
{
QGraphicsScene::mousePressEvent(event);
}
else if(Qt::LeftButton == event->button() && m_mode == ConstructMode)
{
m_clickPositions.append(event->scenePos());
if(m_clickPositions.size() == 2)
{
QLineF lineF(m_clickPositions[0], m_clickPositions[1]);
QGraphicsLineItem* item = this->addLine(lineF);
m_clickPositions.clear();
m_mode = ScrollMode;
}
}
}
I had used something similar in my project and extracted the code. Hope this helps.
Please comment is this is not working.
Edit ::
ConstructMode and Scroll mode are used in the above program so that I can distinguish whether I want to Draw/Construct or just scroll the scene. You can remove them and the declaration of m_mode if not required by you.
If you want to use the modes you can define some public constants and add a method setMode(). Please see the code below.
MyScene.h or some Constant file if you have one
#define ConstructMode 100
#define ScrollMode 101
And add the following function
void MyScene::setMode(int mode)
{
m_mode = mode;
}
After this if you want to enter the construction mode you will need to call myScene->setMode(ConstructMode) everytime, as after the item is constructed the mode is reset to ScrollMode.
I'm using Qt and QProcess to read some data from other tools and printing them on my app. Think of it being a "terminal", for example.
I'm processing data using QProcess::canReadLine() and QProcess:readLine(), and that's wonderful. But some tools use \r to print progress bars on screen, and that's screwing with my parser. Since there is never some line to be read, my app just wait until the process finishes to print the last line: many lines glued together with \r instead of \n.
Anyways, is there someway to tell QProcess to use \r as linebreak also? I thought of implementing my QIODevice subclass, but I'd need to reimplement QProcess too, so that seems to be not the optimal approach.
I thought of using a middle buffer, and use this buffer to signal "hasLine" to my main program. I'd use QProcess::readyRead to populate the buffer, and then the buffer to populate my main app, but I'd like to just tell Qt that a \r is also OK as a linebreak. Is that possible?
I don't think it's possible to directly tell Qt to use '\r' as a linebreak. I thought that QTextStream could do that, but looking at its sources right now it seems to me that I was wrong.
One funny way of doing it would be to implement a custom QIODevice subclass that reads from another QIODevice and just replaces all '\r's with '\n's, delegating all other methods excep read() varieties to the original device. Then readLine() and QTextStream would work on the resulting stream just fine, I think. You'd have to deal somehow with the possible '\r\n' sequence, though. The upside is that you don't have to do any buffering in that class.
Something along these lines:
class CRFilter: public QIODevice {
Q_OBJECT
public:
CRFilter(QIODevice *device);
protected:
virtual qint64 readData(char *data, qint64 maxSize);
virtual qint64 writeData(const char *data, qint64 maxSize);
private:
QIODevice *device;
};
CRFilter::CRFilter(QIODevice *device):
device(device)
{
// delegate the readyRead() signal to this object
connect(device, SIGNAL(readyRead()), SIGNAL(readyRead()));
// and maybe other signals like bytesWritten() too...
}
qint64 CRFilter::readData(char *data, qint64 maxSize)
{
qint64 res = device->read(data, maxSize);
for (qint64 i = 0; i < res; i++) {
if (data[i] == '\r')
data[i] = '\n';
}
return res;
}
qint64 CRFilter::writeData(const char *data, qint64 maxSize)
{
return device->write(data, maxSize);
}
Then you just do this:
QProcess process; // use QProcess methods on this
CRFilter reader(&p); // use QIODevice methods on this
reader.open(QIODevice::ReadWrite); // need this to convince read()/write() methods to work
I hadn't actually tested it, so it probably needs some debugging to get it right. I also think it's a bit ugly, but can't think of any really elegant solution.
Since I'm not using polymorphism with this, no problem inheriting publicly and overriding some methods and signals:
QCLIProcess.h
#ifndef QCLIPROCESS_H
#define QCLIPROCESS_H
#include <QProcess>
class QCLIProcess : public QProcess
{
Q_OBJECT
public:
explicit QCLIProcess(QObject *parent = 0);
bool canReadLine() const;
QString readLine();
signals:
void readyRead();
private slots:
void processLine();
private:
QByteArray buffer;
QStringList lines;
};
#endif // QCLIPROCESS_H
QCLIProcess.cpp
#include "QCLIProcess.h"
#include <QtCore>
QCLIProcess::QCLIProcess(QObject *parent) :
QProcess(parent)
{
setReadChannelMode(QProcess::MergedChannels);
connect((QProcess *)this, SIGNAL(readyRead()), this, SLOT(processLine()));
}
void QCLIProcess::processLine(){
buffer.append(readAll());
int last = 0;
for(int i=0; i<buffer.size(); i++){
if (buffer.at(i) == '\n' || buffer.at(i) == '\r'){
QString line(buffer.mid(last, i-last));
line.append('\n');
if (!line.isEmpty()) lines << line;
last = i+1;
}
}
buffer.remove(0, last);
emit readyRead();
}
bool QCLIProcess::canReadLine() const {
return !lines.isEmpty();
}
QString QCLIProcess::readLine(){
QString line;
if (!lines.isEmpty()){
line = lines.at(0);
lines.removeFirst();
}
return line;
}
UPDATE:
I ended encapsulating the QProcess in a new class, rather than deriving it. This way I could control which signals and which slots I want to expose.
QLineBufferedCRFilteredProcess.h
#ifndef QCLIPROCESS_H
#define QCLIPROCESS_H
#include <QProcess>
class QLineBufferedCRFilteredProcess : public QObject
{
Q_OBJECT
public:
explicit QLineBufferedCRFilteredProcess(QObject *parent = 0);
bool canReadLine() const;
QString readLine();
void start(const QString &program, const QStringList &arguments);
void close();
signals:
void readyRead();
void finished(int exitCode, QProcess::ExitStatus exitStatus);
private slots:
void processLine();
private:
QProcess process;
QByteArray buffer;
QStringList lines;
};
#endif // QCLIPROCESS_H
QLineBufferedCRFilteredProcess.cpp
#include "QLineBufferedCRFilteredProcess.h"
#include
QLineBufferedCRFilteredProcess::QLineBufferedCRFilteredProcess(QObject *parent) :
QObject(parent)
{
process.setReadChannelMode(QProcess::MergedChannels);
connect(&process, SIGNAL(readyRead()), SLOT(processLine()));
connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), SIGNAL(finished(int,QProcess::ExitStatus)));
}
void QLineBufferedCRFilteredProcess::processLine()
{
buffer.append(process.readAll());
int last = 0;
for(int i=0; i<buffer.size(); i++){
if (buffer.at(i) == '\n' || buffer.at(i) == '\r'){
QString line(buffer.mid(last, i-last));
line.append('\n');
if (!line.isEmpty()) lines << line;
last = i+1;
}
}
buffer.remove(0, last);
emit readyRead();
}
bool QLineBufferedCRFilteredProcess::canReadLine() const
{
return !lines.isEmpty();
}
QString QLineBufferedCRFilteredProcess::readLine()
{
QString line;
if (!lines.isEmpty()){
line = lines.at(0);
lines.removeFirst();
}
return line;
}
void QLineBufferedCRFilteredProcess::start(const QString &program, const QStringList &arguments)
{
process.start(program, arguments);
}
void QLineBufferedCRFilteredProcess::close()
{
process.close();
}
I am working on building a GUI around a console application. I would like to be able to click a button to run the console app and show the console output inside of the GUI itself. How might I accomplish this? I am working in Linux.
You could also try QProcess. It provides a Qt interface to launching external processes, reading their I/O and waiting, or not, on their completion.
For your purpose, it sounds like you want the process to run asynchronously, so code might look like :
myprocessstarter.h :
#include <QObject>
#include <QProcess>
#include <QDebug>
class MyProcessStarter : public QObject
{
Q_OBJECT
public:
MyProcessStarter() : QObject() {};
void StartProcess();
private slots:
void readStandardOutput();
private:
QProcess *myProcess;
};
main.cpp:
#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();
}
I wanted to do something similar in one of my applications. I redirected all output from the standard stream (cout) to my console window. To periodically read out the stream contents I use a timer loop. Works fine for me.
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] == '\n')
{
t = t + "\n";
} 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 class is based on code from this forum post: http://www.qtforum.org/article/24554/displaying-std-cout-in-a-text-box.html
Take a look at the popen() function, it might do what you need.
Then you could pass the FILE * to a QTextStream and work in Qt style with it.
I suggest, rather than showing stdout in GUI, having own console output, which essentially means all messages you want to show to users you are sending to your own output.
This way you can have debug messages and such still available from console, wtih potential errors with connections and whatever that can happen and have fully controlled console output in GUI application. Of course this output can also be outputted to stdout so it is visible in console, but it also allows you to append a prefixs like WARNING LOG NOTICE NO_THIS_WENT_WRONG or whatever you want to show to users as your console entry.