Results 1 to 3 of 3

Thread: getting to the buttons inside a QGridLayout

  1. #1
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: getting to the buttons inside a QGridLayout

    Hi,

    Currently, I'm making a little calculator program to train my coding skills. I use the QGridLayout because it helps me creating the layout fast.
    My problem is: I don't know how to connect the buttons in the grid because I don't know how to 'access' them.

    I started of with the code from this tutorial, look for QGridLayout.


    Added after 4 minutes:


    EDIT:
    (The solution I am trying now is to first make an array of buttons and then use those buttons in the grid)
    This code compiles but doesn't run. If I can find the mistake in this piece of code I think I don't have any problems anymore.
    Qt Code:
    1. char *knopLabels[20] = { "7", "8", "9", "/", "sqrt",
    2. "4", "5", "6", "*", "x^y",
    3. "1", "2", "3", "-", "n!",
    4. "0", ",", "+/-", "+", "="};
    5. int labelIndex = 0;
    6. QPushButton *knop [20];
    7. for (int i=0; i<4; i++) {
    8. for (int j=0; j<5; j++){
    9. knop[5*i+j]->setText(knopLabels[labelIndex]);
    10. knop[5*i+j]->setMaximumWidth(50);
    11. myGrid->addWidget(knop[5*i+j], i, j);
    12. labelIndex++;
    13. };
    To copy to clipboard, switch view to plain text mode 


    Added after 4 minutes:


    fixed it:

    Qt Code:
    1. char *knopLabels[20] = { "7", "8", "9", "/", "sqrt",
    2. "4", "5", "6", "*", "x^y",
    3. "1", "2", "3", "-", "n!",
    4. "0", ",", "+/-", "+", "="};
    5. int labelIndex = 0;
    6. QPushButton *knop[20];
    7. for (int i=0; i<4; i++) {
    8. for (int j=0; j<5; j++){
    9. knop[5*i+j] = new QPushButton(knopLabels[labelIndex]);
    10. knop[5*i+j]->setMaximumWidth(50);
    11. myGrid->addWidget(knop[5*i+j], i, j);
    12. labelIndex++;
    13. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by mr_kazoodle; 19th February 2011 at 15:28.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting to the buttons inside a QGridLayout

    Read about QObject::findChild method.

  3. The following user says thank you to Lesiok for this useful post:

    mr_kazoodle (19th February 2011)

  4. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting to the buttons inside a QGridLayout

    Obviosuly you werent creating the button objects itself !!

Similar Threads

  1. Resizing QGraphicsView inside a QGridLayout
    By mckinnon in forum Newbie
    Replies: 3
    Last Post: 9th September 2010, 10:15
  2. QGridLayOut
    By Rakesh_Kumar in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 04:49
  3. Replies: 1
    Last Post: 4th July 2008, 17:17
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  5. resize buttons inside
    By eleanor in forum Qt Programming
    Replies: 8
    Last Post: 21st October 2007, 05:21

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.