Results 1 to 5 of 5

Thread: Changes in buttons' shapes and positions

  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Changes in buttons' shapes and positions

    Hi guys,

    This is a for loop of the code positioning and giving sizes to 22 buttons:

    Qt Code:
    1. for(int i=0; i<texts.size(); ++i)
    2. {
    3. QPushButton* button = new QPushButton(texts[i]);
    4.  
    5. connect(button, SIGNAL(clicked(bool)),
    6. signalMapper, SLOT(map()));
    7. button -> setFixedSize(50,30);
    8.  
    9. signalMapper -> setMapping(button, texts[i]);
    10. gridLayout -> addWidget(button, i/5, i%5);
    11. }
    To copy to clipboard, switch view to plain text mode 

    The image is in the following link:
    http://uploads.im/BCXZu.jpg

    First I want to change the position of the buttons. For example arrange them. And make some ones smaller/bigger that other ones.

    Then to give the buttons some colors to appear nice.

    How to do these please? Should I firstly remove the gridlayout?
    I wrote the app only in C++ code and didn't use the Designer.

  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: Changes in buttons' shapes and positions

    You can use more then one layout (layout in layout). One QGridLayout for digits, second for operators (sum, divide etc.).
    For labels Operations, Results and QLineEdits use QFormLayout. Something like this.
    Attached Images Attached Images

  3. #3
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Changes in buttons' shapes and positions

    Thank you for the reply, but I want to manipulate the size, position, color and the font of each button manually. I think I need to somehow take back the buttons from signalMapper so that I will be able to do the work above on then.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Changes in buttons' shapes and positions

    Instead of throwing away the value of "button" on every pass through your loop, make a QList< QPushButton * > or QVector< QPushButton * > as a member of your class and save them there.

    Qt Code:
    1. // in class definition: QVector< QPushButton * > myButtons
    2.  
    3. myButtons.clear();
    4. for(int i=0; i<texts.size(); ++i)
    5. {
    6. QPushButton* button = new QPushButton(texts[i]);
    7.  
    8. connect(button, SIGNAL(clicked(bool)),
    9. signalMapper, SLOT(map()));
    10. button -> setFixedSize(50,30);
    11.  
    12. signalMapper -> setMapping(button, texts[i]);
    13. gridLayout -> addWidget(button, i/5, i%5);
    14.  
    15. myButtons.push_back( button );
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now that you have saved the button pointers, you can do whatever you want with the button properties. Of course, with fixed size buttons, changing the font will probably result in undesired effects (like the text becoming too big for the button). Why not let the layout do what it is designed to do, and allow the buttons to shrink and grow as the grid does?
    Last edited by d_stranz; 28th January 2017 at 16:34.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Changes in buttons' shapes and positions

    You can use something like this for customized shapes, images etc.

    Qt Code:
    1. QPixmap pixmapLogin(":/new/prefix1/Resources/login_released.png");
    2. QSize imagesize = pixmapLogin.size();
    3. ui->LoginButton->setFixedSize(imagesize);
    4. ui->LoginButton->setMask(pixmapLogin.mask());
    5. ui->LoginButton->setCursor(QCursor(Qt::PointingHandCursor));
    To copy to clipboard, switch view to plain text mode 
    Last edited by Gokulnathvc; 13th September 2017 at 10:53. Reason: Formatting done

Similar Threads

  1. Design buttons with custom shapes
    By LuisLoez89 in forum Qt Programming
    Replies: 7
    Last Post: 22nd April 2015, 16:36
  2. Replies: 0
    Last Post: 30th April 2013, 17:32
  3. computing intersections of two shapes
    By alpinista in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2009, 11:23
  4. Drawing shapes
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2008, 09:19
  5. Irregular window shapes
    By Salazaar in forum Newbie
    Replies: 2
    Last Post: 21st June 2007, 14:52

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.