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. Working with different buttons type
Forum Update on Monday, May 27th 2025

Working with different buttons type

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 320 Views
  • 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.
  • N Offline
    N Offline
    n34rt
    wrote on 1 Oct 2022, 18:56 last edited by
    #1
    template <typename Control>
    void AddQLabel(QLabelStruct* qls, Control* control)
    { 
        QLabel* label = new QLabel(qls->text, control); // <-- this works
        //....
    }
    
    
    
    void AddQLabel(QLabelStruct* qls, QObject* control)
    {
        QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type
       //...
    }
    
    
    
    Main::Main(QWidget* parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        QObject* btn = qobject_cast<QToolButton*>(ui.toolButton);
        AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel
    
        AddQLabel(&qls, ui.toolButton); // <-- works
    }
    

    Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
    without using a template?

    M 1 Reply Last reply 1 Oct 2022, 20:05
    0
    • M Offline
      M Offline
      mpergand
      wrote on 1 Oct 2022, 21:23 last edited by mpergand 10 Jan 2022, 21:25
      #5

      Because widgets need a QWidget* as parent in their constructors.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        n34rt
        wrote on 1 Oct 2022, 19:53 last edited by
        #2

        Got it working using:

        void AddQLabel(QLabelStruct* qls, QAbstractButton* control)
        {
        }
        
        1 Reply Last reply
        0
        • N n34rt
          1 Oct 2022, 18:56
          template <typename Control>
          void AddQLabel(QLabelStruct* qls, Control* control)
          { 
              QLabel* label = new QLabel(qls->text, control); // <-- this works
              //....
          }
          
          
          
          void AddQLabel(QLabelStruct* qls, QObject* control)
          {
              QLabel* label = new QLabel(qls->text, control); // <-- fails because of control type
             //...
          }
          
          
          
          Main::Main(QWidget* parent)
              : QMainWindow(parent)
          {
              ui.setupUi(this);
          
              QObject* btn = qobject_cast<QToolButton*>(ui.toolButton);
              AddQLabel(&qls, btn); // <-- fail cant use QOBject on new QLabel
          
              AddQLabel(&qls, ui.toolButton); // <-- works
          }
          

          Is it possible to write such a function that could work with button, toolbutton, checkbox, etc
          without using a template?

          M Offline
          M Offline
          mpergand
          wrote on 1 Oct 2022, 20:05 last edited by
          #3

          @n34rt said in Working with different buttons type:

          void AddQLabel(QLabelStruct* qls, QObject* control)

          use QWidget instead.

          N 1 Reply Last reply 1 Oct 2022, 21:00
          0
          • M mpergand
            1 Oct 2022, 20:05

            @n34rt said in Working with different buttons type:

            void AddQLabel(QLabelStruct* qls, QObject* control)

            use QWidget instead.

            N Offline
            N Offline
            n34rt
            wrote on 1 Oct 2022, 21:00 last edited by
            #4

            @mpergand thank you, whats the difference?

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mpergand
              wrote on 1 Oct 2022, 21:23 last edited by mpergand 10 Jan 2022, 21:25
              #5

              Because widgets need a QWidget* as parent in their constructors.

              1 Reply Last reply
              0

              1/5

              1 Oct 2022, 18:56

              • Login

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