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. [Solved] Qt4: Connect toolbar buttons to the slot of a promoted widget
Forum Updated to NodeBB v4.3 + New Features

[Solved] Qt4: Connect toolbar buttons to the slot of a promoted widget

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.4k 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.
  • T Offline
    T Offline
    Tay2510
    wrote on last edited by
    #1

    Hi, I am using Qt 4.8.5 with MSVC2010 compiler and windows 7.

    I've successfully promoted one of the QWidget to my custom class using designer in Qt creator.

    Suppose the custom class looks like this:
    @class CustomClass: public QWidget
    {
    ...
    private slots:
    void mySlot();
    ...
    }@

    and suppose the QWidget that is promoted to CustomClass is named :
    PromotedQWidget

    Now I attempt to connect one of the toolbar button to this promoted widget.
    In my mainwindow.cpp, I wroted this code:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "customclass.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    connect(ui->actionToolBarButton, SIGNAL(triggered()),
            ui->PromotedQWidget, SLOT(mySlot()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    Although the auto-completion shows that mySlot exists behind ui->PromotedQWidget,
    I found this connect doe not work. When I clicked the actionToolBarButton, nothing really happened.

    I think there might be something wrong in this way of connection, but I don't know where to start.

    Could someone please give me a hint? Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If the connection fails, you should have an informative message on your console.

      Did you add Q_OBJECT to your CustomClass ?
      Also, why make the slot private since you want to connect it from outside ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tay2510
        wrote on last edited by
        #3

        Thank you SGalst. I got this message: Object::connect: No such slot QCustomPlot::mySlot()

        Also I found it could be the problem of Q_OBJECT.
        My CustomClass doesn't have Q_OBJECT macro. Actually it inherits from another CustomBaseClass which has Q_OBJECT macro and I think it should be inherited too. The weird thing is, after I add Q_OBJECT to CustomClass, the compiler shows error lnk2001 unresolved external symbol.

        I'll look into it. Thanks for your response, it's very informative to me.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You need to put the macro each time you are going to use signals and slots in a QObject derived class. It will allow to create the meta object data needed by Qt to do its magic.

          Is the slot implemented somewhere ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tay2510
            wrote on last edited by
            #5

            The slot is declared in customclass.h, and implemented in customclass.cpp.
            I am still fixing the problem of Q_OBJECT. I cannot put the macro in my CustomClass , which is derived from CustomBaseClass, and the CustomBaseClass is derived from QWidget. In other words, the CustomBaseClass can use Q_OBJECT but the derived CustomClass cannot

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Why can't it ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tay2510
                wrote on last edited by
                #7

                Thank you for the response, I've fixed it!

                Some other slots of the base class are implemented elsewhere. I am restructuring my porgram, its too painful for debugging!

                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