Results 1 to 4 of 4

Thread: Can not create buttons with for loop

  1. #1
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Can not create buttons with for loop

    I tried to create a list of buttons using for loop as follow:
    Qt Code:
    1. Mywidget::Mywidget()
    2. {
    3. QVBoxLayout *h_layout=new QVBoxLayout(this);
    4. QPushButton *close_button=new QPushButton[4];
    5. for (int i = 0; i < 5; ++i) {
    6. close_button[i]=new QPushButton("Close",this);
    7. h_layout->addWidget(close_button);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    But the following error appears:
    Qt Code:
    1. error: no match for 'operator=' (operand types are 'QPushButton' and 'QPushButton*')close_button[i]=new QPushButton("Close",this);
    2. ^
    To copy to clipboard, switch view to plain text mode 
    But if i removed
    Qt Code:
    1. close_button[i]
    To copy to clipboard, switch view to plain text mode 
    and make it only
    Qt Code:
    1. close_button
    To copy to clipboard, switch view to plain text mode 
    it create the required button, but the problem here that i can not use every pointer to do specific thing for example :
    Qt Code:
    1. connect(close_button[2],SIGNAL(clicked(bool)),qApp,SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    Please help

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Can not create buttons with for loop

    *sigh*
    You really should first learn basic syntax and basic concepts of what is an array, and containers of C++.

    Qt Code:
    1. Mywidget::Mywidget()
    2. {
    3. QVBoxLayout *h_layout=new QVBoxLayout(this);
    4. for (int i = 0; i < 5; ++i) {
    5. QPushButton* pButton = new QPushButton("Close",this);
    6. h_layout->addWidget(pButton);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Can not create buttons with for loop

    Qt Code:
    1. QPushButton *close_button=new QPushButton[4];
    To copy to clipboard, switch view to plain text mode 

    This is syntax error in code.

    Normally you create a button like so:
    Qt Code:
    1. QPushButton *pbtn = new QPushButton(this); // this is parent of the button
    2. QPushButton *pbtn = new QPushButton("Title",this); // assigning title and parent
    To copy to clipboard, switch view to plain text mode 

    Both are single buttons. but if u want many buttons in array you can do something like:

    Qt Code:
    1. QPushButton *btnArray[5];
    2. for (i=0;i<5; i++) {
    3. btnArray[i] = new QPushButton(this);
    4. // maybe good to assign object name too
    5. btnArray[i].objectName = "btn" + QString::number(i);
    6. }
    To copy to clipboard, switch view to plain text mode 
    I am noob. Are you noob? Lets learn Qt together! https://qtnoobies.blogspot.com/

  4. The following user says thank you to GeneCode for this useful post:

    Ahmed Abdellatif (29th September 2017)

  5. #4
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: Can not create buttons with for loop

    Thank you

Similar Threads

  1. Replies: 4
    Last Post: 5th June 2014, 22:34
  2. Create PushButtons in Loop
    By homerun4711 in forum Newbie
    Replies: 4
    Last Post: 8th December 2010, 12:39
  3. GLib-ERROR **: Cannot create pipe main loop wake-up
    By chenxuelian in forum Qt Programming
    Replies: 0
    Last Post: 2nd April 2010, 08:18
  4. how to create notification with buttons ?
    By gokul in forum General Programming
    Replies: 2
    Last Post: 28th July 2009, 15:52
  5. create play,pause and stop buttons
    By Sheetal in forum Qt Tools
    Replies: 6
    Last Post: 31st January 2007, 15:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.