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. How to alocate a double pointer to QPushbutton
Forum Updated to NodeBB v4.3 + New Features

How to alocate a double pointer to QPushbutton

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.6k 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.
  • A Offline
    A Offline
    Adrian.Aioanei
    wrote on last edited by Adrian.Aioanei
    #1

    Hey guys,

    I need to create a number of buttons from user input. So i have a class with a member

    QPushButton** buttons;
    

    And then i have a function

    void networkDivide::generateLabelForNetwork(int value)
    {
        networkDivide::numberOfWidgets = value;
        QHBoxLayout* layout = new QHBoxLayout();
        *buttons= new QPushButton[value]();
        for(int i=0;i<value;i++)
        {
            buttons[i] = new QPushButton("Butt");
            layout->addWidget(buttons[i]);
        }
        m_networkGroup->setLayout(layout);
    }
    

    Dosen't give any error but it fails when alocate memory.
    How should I allocate a double pointer of QPushButton type?

    raven-worxR 1 Reply Last reply
    0
    • A Adrian.Aioanei

      Hey guys,

      I need to create a number of buttons from user input. So i have a class with a member

      QPushButton** buttons;
      

      And then i have a function

      void networkDivide::generateLabelForNetwork(int value)
      {
          networkDivide::numberOfWidgets = value;
          QHBoxLayout* layout = new QHBoxLayout();
          *buttons= new QPushButton[value]();
          for(int i=0;i<value;i++)
          {
              buttons[i] = new QPushButton("Butt");
              layout->addWidget(buttons[i]);
          }
          m_networkGroup->setLayout(layout);
      }
      

      Dosen't give any error but it fails when alocate memory.
      How should I allocate a double pointer of QPushButton type?

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

      @Adrian.Aioanei
      most easiest would be to store the pointers in one of Qt's container classes, like QList for example.
      Unless you are dependent on a pointer-to-pointer list?

      --- 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
      1
      • A Offline
        A Offline
        Adrian.Aioanei
        wrote on last edited by
        #3

        I am not dependent of this. All i try to do is to create a number of buttons after a user input.
        I have a label how takes that value and after this, i create a number of buttons.
        How does QList help me in this case? :)

        J.HilkJ 1 Reply Last reply
        0
        • A Adrian.Aioanei

          I am not dependent of this. All i try to do is to create a number of buttons after a user input.
          I have a label how takes that value and after this, i create a number of buttons.
          How does QList help me in this case? :)

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Adrian.Aioanei
          e.g:

          QList<QPushButton *> btnList;
          
          for(int i = 0; i < 10; i++){
              QPushButton *btn = new QPushButton();
              QString text = QString::number(i);
              btn->setText(text);
              btn->setObjectName("MyButton"+text);
             .....
             
              btnList.append(btn);
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Adrian.Aioanei
            wrote on last edited by
            #5

            This looks ok I will try it.
            One more question...the button is created in for loop. This pointer will not be deleted after the function reach "}" ?

            1 Reply Last reply
            0
            • Vinod KuntojiV Offline
              Vinod KuntojiV Offline
              Vinod Kuntoji
              wrote on last edited by Vinod Kuntoji
              #6

              @Adrian-Aioanei
              QPushButton **buttons;

              void networkDivide::generateLabelForNetwork(int value)
              {
              networkDivide::numberOfWidgets = value;
              QHBoxLayout* layout = new QHBoxLayout();
              buttons= new QPushButton *[value];
              for(int i = 0; i < value; i++) {
              buttons[i] = NULL;
              }
              for(int i=0;i<value;i++)
              {
              buttons[i] = new QPushButton("Butt");
              layout->addWidget(buttons[i]);
              }
              m_networkGroup->setLayout(layout);
              }

              C++, Qt, Qt Quick Developer,
              PthinkS, Bangalore

              1 Reply Last reply
              2

              • Login

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