Results 1 to 13 of 13

Thread: How to work with the replaceWidget function from QGridLayout

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default How to work with the replaceWidget function from QGridLayout

    Hi there,

    I created a QGridLayout and willed it with several GroupBoxes. To update the boxes I planned to replace them since I could not find a possibility to change a label within the GroupBox that is part of the QGridLayout.
    In the documentation I found this: QLayoutItem * QLayout::replaceWidget(QWidget * from, QWidget * to, Qt::FindChildOptions options = Qt::FindChildrenRecursively) but I do not understand it. *to is clear, this is my newly created widget but how do I get the *from widget? I know the position in the grid but there is no function that gives me a widget, they only give me items, which is not working.
    Can anybody post some example code that shows how this function is used?

    Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    Can you elaborate why changing a label inside a groupbox would not work just because the groupbox is in a gridlayout?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to work with the replaceWidget function from QGridLayout

    Because they are created during a PushButton event:
    Qt Code:
    1. propLabel[stationIndex] = new QLabel(updateStation(stationIndex));
    To copy to clipboard, switch view to plain text mode 
    And propLabel[sationIndex]->setText(updateStation(index)); in a different PushButton event does not work:
    Qt Code:
    1. .../mainwindow.cpp:150: Fehler: no member named 'propLabel' in 'Ui::MainWindow'
    2. ui->propLabel[activeIndex]->setText(updateStation(activeIndex));
    3. ~~ ^
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    You are trying to address a list, vector or array inside the instance pointed to by "ui", which does not have a variable of the name "propLabel"

    The code for creating the label works then you have that variable in some other instance.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to work with the replaceWidget function from QGridLayout

    Hi,

    thanks _ for the support so far. I am completely lost here.

    As I said the label is within a group box that is added to the grid layout here:
    Qt Code:
    1. ui->cardLayout->addWidget(stationCard[stationIndex], stationIndex/4, stationIndex%4);
    To copy to clipboard, switch view to plain text mode 
    While the program runs these cards need to be updated but so far I was not able to find a way to do that. The only thing that worked was adding a new card to the same coordinates but I guess that stacks them rather than updating them. I was not able to figure out how this replaceWidget function works or how I can access on of the cards.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    Well, you seem to have all your station widgets (groupboxes) in a container named stationCard, probably an array, a vector or a list.

    So given you have a stationIndex for which you want to update a label, you should always be able to get the station card for that index from that container.
    If the class of the station card widget in there has a setter that internally accesses the label, then you should also be able to change the label's text.

    Lets assume your station card widget it something like this
    Qt Code:
    1. class StationCard : public QGroupBox
    2. {
    3. public:
    4. void setStationName(const QString &name)
    5. {
    6. m_nameLabel->setText(name);
    7. }
    8.  
    9. private:
    10. QLabel *m_nameLabel;
    11. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. stationCard[stationIndex]->setStationName(someStationNameString);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    KeineAhnung (30th April 2014)

  8. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to work with the replaceWidget function from QGridLayout

    Hi _,

    thanks for the hint. Actually I do not have a container. The stationCard is the widget and is passed directly to the layout, it is not stored in a container first. I guess the layout does not work as container...
    As you suggested I will create a new class first and add this class to a vector and than have the vector displayed in the layout. Actually this was one of my questions I posted in another thread, how to combine different UI elements in a class.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    Quote Originally Posted by KeineAhnung View Post
    thanks for the hint. Actually I do not have a container. The stationCard is the widget and is passed directly to the layout, it is not stored in a container first. I guess the layout does not work as container...
    The layout is a kind of container, just not as easily addressable by index.
    But your code had stationCard (and previously propLabel) as a container, it is accessed by index in your code snippets.

    Cheers,
    _

  10. #9
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to work with the replaceWidget function from QGridLayout

    So I tired to redo my code but either I get QLayout: Cannot add null widget to QGridLayout/cardLayout or the program crashes.

    From the beginning. I created a new class StationCard. This class is based on the QGroupBox and adds a QLabel to it, right?
    .h
    Qt Code:
    1. #ifndef STATIONCARD_H
    2. #define STATIONCARD_H
    3.  
    4. #include <QLabel>
    5. #include <QGroupBox>
    6. #include <QLayout>
    7.  
    8. class StationCard : public QGroupBox{
    9. public:
    10. StationCard();
    11. QLabel *propLabel;
    12.  
    13. private:
    14.  
    15. };
    16.  
    17. #endif // STATIONCARD_H
    To copy to clipboard, switch view to plain text mode 
    .cpp
    Qt Code:
    1. #include "stationcard.h"
    2.  
    3. StationCard::StationCard(){
    4. QHBoxLayout *layout = new QHBoxLayout;
    5. layout->addWidget(propLabel, 10, Qt::AlignRight);
    6. setLayout(layout);
    7. }
    To copy to clipboard, switch view to plain text mode 
    In my main window .h I added the .h file of the new class and crated a vector:
    Qt Code:
    1. QVector<StationCard*> stationVector;
    To copy to clipboard, switch view to plain text mode 
    In my main window .cpp I first want to fill my grid with empty cards:
    Qt Code:
    1. stationVector.resize(8);
    2. int k = 0;
    3. for(int i = 0; i < 2; ++i){
    4. ui->cardLayout->setRowMinimumHeight(i,200);
    5. for(int j = 0; j < 4; ++j){
    6. ui->cardLayout->setColumnMinimumWidth(j, 150);
    7. ui->cardLayout->addWidget(stationVector[k], i, j);
    8. k++;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    But this already gives me QLayout: Cannot add null widget to QGridLayout/cardLayout and if I want to fill it, the program crashes. If I say stationVector.resize(8), does this not create an array with eight elements of my custom group box class? If I try to do stationVector[k] = new StationCard, it crashes right away. =(


    Added after 5 minutes:


    Quote Originally Posted by anda_skoa View Post
    But your code had stationCard (and previously propLabel) as a container, it is accessed by index in your code snippets.
    _
    Actually no. I followed a tutorial here where they created several buttons using a for-loop. http://doc.qt.digia.com/4.4/layouts-basiclayouts.html
    Last edited by KeineAhnung; 30th April 2014 at 09:36.

  11. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to work with the replaceWidget function from QGridLayout

    QLayout: Cannot add null widget to QGridLayout/cardLayout or the program crashes.
    Because you have to create the label instance
    Qt Code:
    1. StationCard::StationCard(){
    2. QHBoxLayout *layout = new QHBoxLayout;
    3.  
    4. propLabel = new QLabel(/*parameters*/);
    5.  
    6. layout->addWidget(propLabel, 10, Qt::AlignRight);
    7. setLayout(layout);
    8. }
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to stampede for this useful post:

    KeineAhnung (30th April 2014)

  13. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    As you found out yourself already, you need of course create instances of StationCard before you can add them.

    Qt Code:
    1. stationVector[k] = new StationCard
    To copy to clipboard, switch view to plain text mode 

    You experience a crash because your StationCard constructor tries to add an uninitialized pointer (propLabel) to a layout.
    Create a QLabel instance first

    Cheers,
    _

    P.S.: you can do the StationCard in designer if you want to, just use the widget template for a designer form and add the "ui" pointer and related code lines manually to your class

  14. The following user says thank you to anda_skoa for this useful post:

    KeineAhnung (30th April 2014)

  15. #12
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to work with the replaceWidget function from QGridLayout

    Thanks for the help! Now the program is running and I can tackle the next step to connect the checkbox from the group box. As you can see I am missing a lot of the basic stuff but I am just not good at reading books. I always try to start in the middle...

  16. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to work with the replaceWidget function from QGridLayout

    You can "forward" signals

    Qt Code:
    1. class StationCard : public QGroupBox
    2. {
    3. signals:
    4. void somethingToggled(bool on);
    5. };
    6.  
    7.  
    8. StationCard::StationCard()
    9. {
    10. connect(someCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(somethingToggled(bool)));
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 15
    Last Post: 23rd December 2013, 06:38
  2. Replies: 7
    Last Post: 30th August 2013, 14:01
  3. Replies: 0
    Last Post: 6th November 2011, 14:27
  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. qSort doesn't work with member function
    By ber_44 in forum Qt Programming
    Replies: 10
    Last Post: 2nd June 2007, 12:00

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.