PDA

View Full Version : Letting Widgets from different files get to know each other



FlashMuller
13th May 2010, 15:28
Hey everybody,
heres my Problem: I've got a tiny application consisting of 3 windows, each defined by a .h and a .cpp file. Let's focus onto two of those, the main window and the "viewport", a simple window supposed to show previously computed graphics. The Viewport is, although defined with its own header and cpp-file, started from within the Main Window, so its function is being called inside one of the functions of the main window, so in a different file than it is being defined. Anyway the drawing itself / the paint event is being handled by the viewport "inside" the viewport-files. I hope that was somehow clear.
My Problem is: If I start the paint-event as "QPainter painter(this);" the painting is being done inside the main-window, not inside the viewport (probably because "this" in the structure of my programm relates to the main window). If I try anything like QPainter painter(Viewport) <- name of the Viewport-Window, nothing happens at all, probably because the widget it relates to is being startet inside the Main Window Files and Functions. I hope this makes my point clear, as I will have to cope with it some more often: How do I let my function (paint) in File A know about the Window created in File B?
Thanks and Best
Daniel

tbscope
13th May 2010, 15:40
Hey everybody,
heres my Problem: I've got a tiny application consisting of 3 windows, each defined by a .h and a .cpp file. Let's focus onto two of those, the main window and the "viewport", a simple window supposed to show previously computed graphics. The Viewport is, although defined with its own header and cpp-file, started from within the Main Window, so its function is being called inside one of the functions of the main window, so in a different file than it is being defined. Anyway the drawing itself / the paint event is being handled by the viewport "inside" the viewport-files. I hope that was somehow clear.
This is clear to me.


My Problem is: If I start the paint-event as "QPainter painter(this);" the painting is being done inside the main-window, not inside the viewport (probably because "this" in the structure of my programm relates to the main window). If I try anything like QPainter painter(Viewport) <- name of the Viewport-Window, nothing happens at all, probably because the widget it relates to is being startet inside the Main Window Files and Functions. I hope this makes my point clear, as I will have to cope with it some more often: How do I let my function (paint) in File A know about the Window created in File B?
This isn't clear to me. Where do you write QPainter painter(this); ?
Can you show some code?

wysota
13th May 2010, 16:05
You can only paint from within the paintEvent() of the widget being painted.

FlashMuller
13th May 2010, 19:25
Hey,
this is my file "Zeichenfläche.cpp" (in english Viewport.cpp)
-----------------------------------------------------------------------------------------------------
#include "Zeichenflaeche.h"

Zeichenflaeche::Zeichenflaeche(QMainWindow *parent):QWidget(parent)
{

erstelleZeichenflaeche(parent);

}

//create Widget / Viewport
void Zeichenflaeche::erstelleZeichenflaeche(QMainWindow *parent)
{

Zeichenfl = new QWidget(parent);
Zeichenfl->setWindowFlags(Qt::Window);
Zeichenfl->setWindowTitle("Viewport");
Zeichenfl->show();
}

//Paintevent
void Zeichenflaeche::paintEvent(QPaintEvent *)
{

QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.translate(0, rect().height());
painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16);
}
--------------------------------------------------------------
The window is being "startet" in the file mainwindow.cpp, now just showing the necessary code:
----------------------------------------------------------------
//"Wrapper" for the Mainwindow-Functions
Hauptfenster::Hauptfenster()
{

Toolb = new Toolbar(this);
-----> Zeichenfl = new Zeichenflaeche(this); <-------------- //creation of the Viewport
setCentralWidget(Toolb);
erstelleAktionen();
erstelleIcons();
}
---------------------------------------------------------------
The Painting is actually happening in the Main-window, although it is supposed to happen in the Viewport. Anymore code required? Hope it made everything a little more clear :-)
Best

EDIT: Just to make it really clear: As far as I can see the problem is, that I can't really tell the Paintevent to Paint anywhere else than in the Main Window, which would be really handy :-)

SixDegrees
13th May 2010, 21:12
I can't really tell what you're postulating here, but if you think there's a problem because your code is in seperate files, there's a very simple experiment that will tell you whether this is truly a problem: put them all in a single file. Or a single .h and a single .cpp file. If your problem goes away, it's file related.

I doubt that's the case, but without seeing all of your actual code its difficult to say.

wysota
13th May 2010, 23:20
I don't get the part about painting happening in main window and not in the viewport. The viewport widget clearly has paintEvent() implemented so painting definitely takes place in the viewport widget. Could you say what exactly the problem is? Is it a compilation error or does the code do something different than you expect it to?