Results 1 to 9 of 9

Thread: Creating an Array of Button

  1. #1
    Join Date
    Apr 2011
    Posts
    25
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

    Default Creating an Array of Button

    hello friends

    I am creating an array of buttons having 7 rows 7 colums..
    now i trying to display first seven buttons in one row after which it goes to second row and display next seven button..and so on..

    i have wrriten a code for this..its working but problem is that all the buttons are displayed in one row only..so if anyone suggest me to resolve that problem..

    i am sending my code along with this..

    Qt Code:
    1. QWidget *centralWidget = new QWidget;
    2. selectedDate=QDate::currentDate();
    3. int count=1,i,j;
    4. QPushButton *button[10][10];
    5. QHBoxLayout *controlsLayout = new QHBoxLayout;
    6. for(i=0;i<7;i++)
    7. {
    8. for(j=0;j<7;j++)
    9. {
    10. if(count<=42)
    11. {
    12.  
    13.  
    14. button[i][j] = new QPushButton("p");
    15.  
    16. button[i][j]->resize(40,40);
    17.  
    18. button[i][j]->move(40*j, 40*i);
    19.  
    20. button[i][j]->show();
    21.  
    22. controlsLayout->addWidget(button[i][j]);
    23.  
    24. centralWidget->setLayout(controlsLayout);
    25.  
    26. setCentralWidget(centralWidget);
    27.  
    28. count++;
    29. }
    30.  
    31. }
    32.  
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 21st April 2011 at 15:04. Reason: code tags

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating an Array of Button

    just use a gridlayout for this.

  3. #3
    Join Date
    Apr 2011
    Posts
    25
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

    Default Re: Creating an Array of Button

    I hv try it using grid layout also..

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating an Array of Button

    then your doing it wrong.

    Alternatively, you could a QVBoxLayout and a QHBoxLayout for each line.

  5. #5
    Join Date
    Apr 2011
    Posts
    25
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

    Default Re: Creating an Array of Button

    hello squidge

    yeh u r right..its working now by using grid layout..now hw can i decrease the space between each rows..moreever hw can i add textBrowser below..after all buttons are arranged in rows and colums..

    my code is:u plz check it..
    Qt Code:
    1. QWidget *centralWidget = new QWidget;
    2. selectedDate=QDate::currentDate();
    3. int count=1,i,j;
    4.  
    5. QPushButton *button[10][10];
    6. QGridLayout *controlsLayout = new QGridLayout;
    7. [B]QTextBrowser *T=new QTextBrowser() //how can i add this text browser below after all the array of buttons are set[/B]
    8. for(i=0;i<7;i++)
    9. {
    10.  
    11. for(j=0;j<7;j++)
    12. {
    13.  
    14.  
    15. if(count<=42)
    16. {
    17.  
    18. button[i][j] = new QPushButton(QString::number(count));
    19.  
    20. button[i][j]->resize(40,40);
    21.  
    22. button[i][j]->move(40*j, 40*i);
    23.  
    24. button[i][j]->show();
    25.  
    26. controlsLayout->addWidget(button[i][j],i,j);
    27.  
    28.  
    29.  
    30. controlsLayout->setSpacing(0);
    31.  
    32. centralWidget->setLayout(controlsLayout);
    33.  
    34. setCentralWidget(centralWidget);
    35.  
    36. count++;
    37. }
    38.  
    39. }
    40.  
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 



    with regards
    Anshuman
    Last edited by high_flyer; 21st April 2011 at 15:04. Reason: code tags

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating an Array of Button

    I suggest to read some tutorials about layouts...

  7. #7
    Join Date
    Apr 2011
    Posts
    25
    Qt products
    Qt/Embedded
    Platforms
    Symbian S60

    Default Re: Creating an Array of Button

    hello nish

    yeh by using gridlayout its workin now...the only thing is that …hw can i decrease the space between each row…
    As i had used all this in my code …but its not working..
    QGridLayout::setSpacing()
    QGridLayout ::setHorizontalSpacing()
    QGridLayout ::setVerticalSpacing()

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating an Array of Button

    The easiest way would be to do the same effect in designer and edit the properties until you get your desired effect and then check the generated output. The designer properties should also give you a clue as to what needs changing.

    Or you could just read the documentation of QWidget which QPushButton inherits.

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Creating an Array of Button

    use setContentMargins() for spacing.

Similar Threads

  1. Array of button for creating custom calender
    By anshumanBorah in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2011, 13:21
  2. Creating a style for a button
    By Luc4 in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2010, 13:16
  3. Creating a button programmatically
    By Luc4 in forum Newbie
    Replies: 1
    Last Post: 27th January 2010, 10:30
  4. Creating a Pixmap out of an array of data
    By toratora in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 19:00
  5. Creating a global array in my code???
    By therealjag in forum General Programming
    Replies: 5
    Last Post: 13th March 2006, 11:13

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.