Results 1 to 6 of 6

Thread: Letting Widgets from different files get to know each other

  1. #1
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Letting Widgets from different files get to know each other

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Letting Widgets from different files get to know each other

    Quote Originally Posted by FlashMuller View Post
    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?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Letting Widgets from different files get to know each other

    You can only paint from within the paintEvent() of the widget being painted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Letting Widgets from different files get to know each other

    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:aintEvent(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 :-)
    Last edited by FlashMuller; 13th May 2010 at 19:57.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Letting Widgets from different files get to know each other

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Letting Widgets from different files get to know each other

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 18th April 2010, 23:31
  2. Replies: 12
    Last Post: 17th June 2009, 05:34
  3. Replies: 0
    Last Post: 15th May 2009, 15:38
  4. Replies: 6
    Last Post: 18th December 2008, 21:16
  5. visual studio project files - adding extra files
    By luf in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 21:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.