PDA

View Full Version : The gui is not start



ugiwgh
30th October 2014, 08:17
When I start my program. It is run, but the paintEvent() is not executed. So the gui is not start.
Any help will be appreciated.

Qt: 4.5.1
OS: redhat 6.0

Sometimes it outputs the following log.
X Error: BadIDChoice (invalid resource ID chosen for this connection) 14
Major opcode: 55 (X_CreateGC)
Resource id: 0x2023c
X Error: BadLength (poly request too large or internal Xlib length error) 16
Major opcode: 11 (X_UnmapSubwindows)
Resource id: 0x2023c
X Error: BadRequest (invalid request code or no such operation) 1
Extension: 255 (Uknown extension)
Minor opcode: 0 (Unknown request)
Resource id: 0x2023c

anda_skoa
30th October 2014, 12:21
Are you calling show() on the window/widget?
Are you calling exec() on the application?
Can you reproduce with a minimal main() that uses a standard widget, e.g. a QLabel?

Cheers,
_

ugiwgh
1st November 2014, 04:35
My code is following.



int main(int argc,char **argv)
{
qDebug()<<"Main Begin";
QApplication app(argc,argv);

QWidget *widget=new QWidget;

widget->show();
//widget->showFullScreen();

app.exec();
delete widget;
qDebug()<<"Main End";
return 0;
}

anda_skoa
1st November 2014, 13:35
That should work.
It will be a very tiny window since you did not resize the widget, but it should show.

Cheers,
_

ugiwgh
2nd November 2014, 01:42
I add QPixmap in GUI slot function. Maybe this is the root cause, because when I remove it, the GUI show.

anda_skoa
2nd November 2014, 13:02
Any relevant code you'd like to share?

Cheers,
_

aamer4yu
3rd November 2014, 08:08
When I start my program. It is run, but the paintEvent() is not executed. So the gui is not start.

But from the code you shared -


QWidget *widget=new QWidget;

widget->show();


You are creating a normal QWidget, and not a custom widget.
So where do you expect the paintEvent to get hit ? How are you checking if paintEvent was hit,, and of which class ?

ugiwgh
5th November 2014, 05:00
Yes, the following is the simple code.

main.h



class MRender : public QThread
{
Q_OBJECT
public:
MRender(){}
signals:
void renderedPixmap(const QImage &image);
protected:
void run();
};

class MCanvas : public QWidget
{
Q_OBJECT
public:
MCanvas(){}
public slots:
updatePixmap(const QImage &image);
protected:
void paintEvent(QPaintEvent *e);
private:
QPixmap pixmap_;
};


main.cpp


#include "main.h"

MRender::run()
{
while(1)
{
QPainter painter;
QImage image(400,400,QImage::Formage_RGB32);
image.fill(0xFFFFFF);
painter.begin(&image);
QRectF textRect(0,0,400,400);
painter.drawText(textRect,Qt::AlignCenter,QString("Who are you?"));
painter.end();
emit renderedPixmap(image);
sleep(1);
}
}

MCanvas::updatePixmap(const QImage &image)
{
pixmap_=QPixmap::fromImage(image);
}
MCanvas::paintEvent(QPaintEvent *e)
{
QPianter painter;
painter.begin(this);
painter.drawPixmap(0,0,pixmap_);
painter.end();
}

int main(int argc,char **argv);
{
QApplication app(argc,argv);

MCanvas canvas=new MCanvas;
MRender render=new MRender;

QObject::connect(render,SIGNAL(renderedPixmap(cons t QImage &)),canvas,SLOT(updatePixmap(const QImage &)),Qt::DirectConnection);

QWidget *widget=new QWidget;
QGridLayout *layout=new QGridLayout(widget);
layout->addWidget(canvas);
widget->setLayout(layout);


widget->show();
app.exec();
delete widget;
return 0;
}


The porblem is "pixmap_=QPixmap::fromImage(image);" and "painter.drawPixmap(0,0,pixmap_);".

wysota
5th November 2014, 09:06
You cannot create pixmap in threads other than the main thread. Your connect in line #38 cannot be a Qt::DirectConnection, it has to be Qt::QueuedConnection.

ugiwgh
5th November 2014, 10:20
If use Qt::QueuedConnection, when I resize the window many times, but not release mouse, the buffer queued. And as I resize the window, it take up memory will be more and more, until I release mouse.

And why you think it should use Qt::QueuedConnection?

wysota
5th November 2014, 10:25
If use Qt::QueuedConnection, when I resize the window many times, but not release mouse, the buffer queued.
When you don't, your program doesn't work.


And why you think it should use Qt::QueuedConnection?
Because if you use direct connection, the slot is executed in the context of the caller, which happens to be the worker thread which cannot access pixmaps. I don't know where you copied the code from but if you have to copy someone's code then at least try to understand what it does.

anda_skoa
5th November 2014, 11:40
Also you are missing a call to update() in updatePixmap().

Cheers,
_

ugiwgh
6th November 2014, 03:44
I don't know where you copied the code from but if you have to copy someone's code then at least try to understand what it does.


It's my code.
I code it by myself.
I think you do not clear understand what the code mean.

And when use Qt::QueuedConnection, it call paintEvent, until I release mouse. So the program will be take up more and more memory, and when big enough it will crash.

Added after 4 minutes:



Also you are missing a call to update() in updatePixmap().


Thanks for your remind.
I forgot to add it to the example code. In my program, it's there.

Added after 53 minutes:



Because if you use direct connection, the slot is executed in the context of the caller,


I have test Qt::QueuedConnection. when the GUI slot(in my code is updatePixmap()) is blocked, the program is not start(the gui blocked) too. Authough the signal thread is running.

ugiwgh
6th November 2014, 08:30
which happens to be the worker thread which cannot access pixmaps.

Yes, the QPixmap is a problem, so I change it to QImage.

wysota
6th November 2014, 10:42
I think you do not clear understand what the code mean.
Apparently I don't. I surely lack the wisdom to implement a thread which constantly generates images nobody wants instead of simply storing the last generated image somewhere and letting the other thread only pick up the latest image if it needs it.

ugiwgh
7th November 2014, 11:50
implement a thread which constantly generates images nobody wants instead of simply storing the last generated image somewhere and letting the other thread only pick up the latest image if it needs it.


Great. You say very great right.
I didn't cache them now. Maybe I'll do that in future.
I code it step by step now. And didn't have enought time to complete it immediately.