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. Connecting a children widget with main widget
Qt 6.11 is out! See what's new in the release blog

Connecting a children widget with main widget

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 8.2k Views 1 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.
  • B Offline
    B Offline
    blixs
    wrote on last edited by
    #1

    Hi!

    I am a Qt-newbie, and need some help. I have to make a kind of simple Painter, and I have a one main widget where I have menubar and 2 buttons. One for draw a line, and one for rectangles. The problem I have, is how to connect those two actions with the happenings in the children widget.
    I thought first that I can do a slot-method that return some kind of boolean, and in the canvaswigdet class make a check if its drawline or drawrectangle. But that didn't work.

    Can somebody help me?
    I used QtCreator with QtDesigner for the GUI.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Your idea to use signals and slots is a good one. That is the way to deal with button clicks. However, slot-methods usually do not return a value. They are usually declared void, and only take arguments. What you are after, I think, is some kind of drawing mode selection. Look into QButtonGroup to make a set of exclusive (tool) buttons. In your drawing code, you check in which mode you are. It depends a bit where your drawing code is located (in the main widget code, or in your canvas widget code) how you should communicate this state.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Or just define two slots drawLine() and drawRect() that you connect the respective buttons to.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blixs
          wrote on last edited by
          #4

          Can I have define those slots in the prototype of mainwindow and access them from the children widget?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Children widget? What widget would that be?

            What do you mean by "access"? Call them? Sure, if your "Children widget" has a reference to the mainwindow and the slots are active, you can call them. But I doubt that is what you are after.

            If your "children widget" is the widget responsible for the drawing, you probably want to define the slot(s) on that widget directly. Then, you can connect the signal from your buttons (or button group) to that slot or those slots from the mainwidget's constructor.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              blixs
              wrote on last edited by
              #6

              Yeah, children widget is the canvas widget.

              So I should define the slots directly there. Will not be a problem even the actions come from the MainWindow_ui?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                You define the slots in the class that will do the drawing.

                You only can define slots in UI components that have a C++ class on its own, not on widgets that are just child widgets of some other UI.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tekblom
                  wrote on last edited by
                  #8

                  Hi,
                  I am colloborating with blixs. Thanks for your answers, wanted to make it a bit clearer...

                  This is in our ui_mainwindow.h
                  @ void setupUi(QMainWindow *MainWindow)
                  {...
                  actionLinea = new QAction(MainWindow);
                  ...
                  centralWidget = new PanelDibujo(MainWindow);
                  ...
                  QObject::connect(actionLinea, SIGNAL(hovered()), centralWidget, SLOT(linea()));
                  }@

                  And should trigger in our childwidget paneldibujo.cpp
                  @void PanelDibujo::linea()
                  {
                  lineaMarked = true;
                  }@

                  But nothing happens...
                  It works with SLOT-methods like hide() etc...

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    is linea() declared as slot?
                    Did you check debugging output if there are errors / warnings?

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tekblom
                      wrote on last edited by
                      #10

                      No, it is not declared as a slot. You need to do that somewhere?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        What does the console output show? Any errors about connections?

                        It should work too, if you declare linea() with the "public/protected/private slots" pseudo keyword.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          [quote author="tekblom" date="1295269403"]No, it is not declared as a slot. You need to do that somewhere?[/quote]

                          Yes, you must add that in the class definition (usually in a .h file). See the "Signals & Slots":http://doc.qt.nokia.com/stable/signalsandslots.html docs for some examples. Unfortunately the docs say it not clear enough, that you must add the "public/protected/private slots" keywords.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tekblom
                            wrote on last edited by
                            #13

                            Oh, yeah I found it now.

                            Object::connect: No such slot PanelDibujo::linea() in ./ui_mainwindow.h:109
                            Object::connect: (sender name: 'actionLinea')
                            Object::connect: (receiver name: 'centralWidget')

                            I added in paneldibujo.h
                            @ public slots:
                            void linea();@

                            and the error disappeared but still nothings happens. Use the same code as before...

                            But now...Wow it works ;)
                            Had to rebuild it some times!

                            Thanks for the help!

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              You should at least call qmake after you add the first signal or slot to your class to catch up the changes.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              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