Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMainWindow paint event
Forum Updated to NodeBB v4.3 + New Features

QMainWindow paint event

Scheduled Pinned Locked Moved General and Desktop
qmainwindow
11 Posts 4 Posters 5.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Gianluca86G Offline
    Gianluca86G Offline
    Gianluca86
    wrote on last edited by
    #1

    Hello, sorry for the stupid question, but the Main window has a paint function? That is, a function that starts once the window open.
    What I would do is this:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {}
    
    MainWindow::paint()
    {
        CreateActions();
        CreateToolBars();
    }
    

    For now I only found a similar method

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
     QTimer::singleShot(1, this, SLOT(paint()));
    }
    
    MainWindow::paint()
    {
        CreateActions();
        CreateToolBars();
    }
    

    Thanks

    raven-worxR ? 2 Replies Last reply
    0
    • Gianluca86G Gianluca86

      Hello, sorry for the stupid question, but the Main window has a paint function? That is, a function that starts once the window open.
      What I would do is this:

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {}
      
      MainWindow::paint()
      {
          CreateActions();
          CreateToolBars();
      }
      

      For now I only found a similar method

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
       QTimer::singleShot(1, this, SLOT(paint()));
      }
      
      MainWindow::paint()
      {
          CreateActions();
          CreateToolBars();
      }
      

      Thanks

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Gianluca86
      what is the question?!
      And what are you actually trying to do? There is no such paint() method...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Gianluca86G Offline
        Gianluca86G Offline
        Gianluca86
        wrote on last edited by
        #3

        In C# windows can be created "empty", then it is called a design tool that "draws" all objects, such as buttons, toolbar, etc.
        This is to avoid calling a function in the constructor of mainwindow that can crash even before the window is created.
        I hope I explained better.

        raven-worxR 1 Reply Last reply
        0
        • Gianluca86G Gianluca86

          In C# windows can be created "empty", then it is called a design tool that "draws" all objects, such as buttons, toolbar, etc.
          This is to avoid calling a function in the constructor of mainwindow that can crash even before the window is created.
          I hope I explained better.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Gianluca86 said:

          I hope I explained better.

          no not really, sorry.
          Why not calling your paint() method - which seems to me like a n ordinary init method? Does it crash in your case?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • Gianluca86G Offline
            Gianluca86G Offline
            Gianluca86
            wrote on last edited by
            #5

            Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
            I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it

            raven-worxR kshegunovK 2 Replies Last reply
            0
            • Gianluca86G Gianluca86

              Hello, sorry for the stupid question, but the Main window has a paint function? That is, a function that starts once the window open.
              What I would do is this:

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {}
              
              MainWindow::paint()
              {
                  CreateActions();
                  CreateToolBars();
              }
              

              For now I only found a similar method

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
               QTimer::singleShot(1, this, SLOT(paint()));
              }
              
              MainWindow::paint()
              {
                  CreateActions();
                  CreateToolBars();
              }
              

              Thanks

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #6

              @Gianluca86 said:

              That is, a function that starts once the window open.

              Note that your current implementation in general doesn't execute your paint method when the window becomes visible ("once the window open") for the first time but already after the instantiation of the window.

              You may want to connect to the void visibleChanged(bool arg) signal.

              { // inside constructor
                connect(this, &QMainWindow::visibleChanged, this, &QMainWindow::paint);
              }
              
              { // inside paint method
                do_some_init_stuff(); 
                disconnect(this, &QMainWindow::visibleChanged, this, &QMainWindow::paint);  
              }
              1 Reply Last reply
              0
              • Gianluca86G Gianluca86

                Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
                I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #7

                @Gianluca86 said:

                Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.

                normally you are fine to create/init your ui in the constructor - unless you have a good reason to delay it of course.
                Anyway the delaying similar to you've done is ok in such case:

                QmetaObject::invokeMethod(this, "delayedCalledMethodName", Qt::QueuedConnection);
                

                Or even later, like the example shown by @Wieland

                I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it

                update() needs to be called whenever you need a visual (painting) update of your widget. A paint event is scheduled in the global event queue and redrawn in the next event loop iteration. Multiple update() calls are optimized here, and just a single paint event will be delivered.
                If you need an immediate update you can call QWidget::repaint(). But normally update() is the way to go.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • Gianluca86G Gianluca86

                  Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.
                  I have a question: the "void QWidget :: update ()" how does it work? Is there an example to look at? Unfortunately, I understand the concept but not how to use it

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @Gianluca86

                  Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.

                  There's no need. Qt's classes have well behaved constructors that will not throw exceptions. Additionally, Qt doesn't really create the GUI elements in the constructors, there's a dedicated method that does that and it's called after the constructors have run.

                  Kind regards.

                  Edit: Ignore erroneous statements, please.

                  Read and abide by the Qt Code of Conduct

                  raven-worxR 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @Gianluca86

                    Yes I can. I wanted to know if in the windows in Qt had a similar function like C#, and from what I understand there is not.

                    There's no need. Qt's classes have well behaved constructors that will not throw exceptions. Additionally, Qt doesn't really create the GUI elements in the constructors, there's a dedicated method that does that and it's called after the constructors have run.

                    Kind regards.

                    Edit: Ignore erroneous statements, please.

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    @kshegunov
                    QWidget::create() isn't virtual and thus not meant to be overloaded and anyway not meant to be used this way.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    kshegunovK 1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @kshegunov
                      QWidget::create() isn't virtual and thus not meant to be overloaded and anyway not meant to be used this way.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @raven-worx
                      I didn't say it's supposed to be overridden or be used by the user.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • Gianluca86G Offline
                        Gianluca86G Offline
                        Gianluca86
                        wrote on last edited by
                        #11

                        Thank you all, I will try to use your advice.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved