[Kde-bindings] Qyoto: Reading from stdin from a gui-application
Christoph Spielmann
spielc at gmail.com
Sun May 18 18:21:47 UTC 2008
Arno Rehn schrieb:
> Am Sonntag 18 Mai 2008 16:14:39 schrieb Arno Rehn:
>
>> Am Sonntag 18 Mai 2008 15:52:29 schrieb Christoph Spielmann:
>>
>>> I tried out the fix you proposed and now the error message is gone but
>>> it's still not working as expected. If I use QTextStream as proposed by
>>> qt-documentation nothing happens (e.g when i use the Read*-methods of
>>> QTextStream) or i get a endless-loop if i use AtEnd() for example. When
>>> i use QFile directly using the Read*-methods i get very strange results:
>>>
>>> Read(string,long): nothing happens
>>> ReadLine(string,long): the method returns the correct length of bytes
>>> available but the string it's supposed to append the bytes to is not
>>> changed
>>>
> This is because these read methods expect a char*, that means a reference to a
> string in which the data is stored. In Qyoto the code is wrongly generated,
> so it won't work.
>
>
>>> ReadAll(): nothing happens
>>>
> works for me.
>
>
>>> So i guess i found another problem... ;)
>>>
>> Strange. Have you tried to do it in C++ and does it work there? It would
>> also be helpful if you sent the source of this, so we can test it.
>>
Okey i wrote a short testapplication that should demonstrate what i want
to accomplish. I wrote it in both c++ and c#.
hello-qt4.h
#include <QWidget>
#include <QFile>
#include <QSocketNotifier>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
public slots:
void MyClose();
void DataReadyOnStdin();
private:
QFile* stdin;
QSocketNotifier* stdinNot;
};
hello-qt4.cpp:
#include <QApplication>
#include <QPushButton>
#include <stdio.h>
#include "hello-qt4.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);
QPushButton *quit = new QPushButton(tr("Quit"), this);
quit->setGeometry(62, 40, 75, 30);
quit->setFont(QFont("Times", 18, QFont::Bold));
connect(quit, SIGNAL(clicked()), this, SLOT(MyClose()));
stdin=new QFile();
stdin->open(0,QIODevice::ReadOnly);
stdinNot=new QSocketNotifier(0,QSocketNotifier::Read);
connect(stdinNot,SIGNAL(activated(int)),this,SLOT(DataReadyOnStdin()));
}
void MyWidget::MyClose() {
printf("hello world closing!\n");
exit(0);
}
void MyWidget::DataReadyOnStdin() {
char* buf=(char*)malloc(255*sizeof(char));
stdin->readLine(buf,(qint64)sizeof(buf));
printf("%s\n",buf);
free(buf);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
//connect(quit, SIGNAL(clicked()), widget, SLOT(MyClose()));
/*QPushButton hello("Hello world!");
hello.resize(100, 30);
QObject::connect(&hello, SIGNAL(clicked()), &app, SLOT(quit()));
hello.show();*/
return app.exec();
}
This implementation is doing exactly what i want. And now the same thing
in C#:
using System;
using Qyoto;
namespace qttest1
{
class MyWidget:QWidget
{
private QFile stdin;
private QSocketNotifier stdinNot;
public MyWidget():base() {
this.SetFixedSize(200, 120);
QPushButton quit=new QPushButton("Quit",this);
quit.SetGeometry(62, 40, 75, 30);
QObject.Connect(quit, Qt.SIGNAL("clicked()"), qApp,
Qt.SLOT("quit()"));
this.stdin=new QFile();
this.stdin.Open(0,(int) QIODevice.OpenModeFlag.ReadOnly);
this.stdinNot=new
QSocketNotifier(0,QSocketNotifier.TypeOf.Read);
QObject.Connect(stdinNot,Qt.SIGNAL("activated(int)"),this,Qt.SLOT("DataReadyOnStdin()"));
}
[Q_SLOT]
public void DataReadyOnStdin() {
string buf="";
this.stdin.ReadLine(buf,(long)255);
Console.WriteLine(buf);
}
public static int Main(string[] args)
{
new QApplication(args);
MyWidget widget=new MyWidget();
widget.Show();
return QApplication.Exec();
}
}
}
Here everything works except reading from stdin (means
this.stdin.ReadLine(buf,(long)255);). When i try to replace ReadLine
with ReadAll the UI becomes unresponsive and that should not happen in
my application.
Some more info on my setup:
I'm on Gentoo/AMD64 (i can almost here the oh-noes here ;) ) here, with
QT 4.3.4 (qt-4.4 has not hit portage yet) and kde-4.0.4 with manually
compiled kdebindings (another thing that's not in the tree, quite a
shame in my eyes but okey i can help myself ;) )
So well i hope anyone can make sense of all this and help me out.
Christoph
>
>
>
More information about the Kde-bindings
mailing list