Results 1 to 4 of 4

Thread: need help in getting a logic to work

  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default need help in getting a logic to work

    Hi all,

    I have this problem which I have been pondering and can't seem to find a solution to it. Therefore, I need some expertise in programming field for help.

    I have a group of widget (1 label, 1 combo Box, 1 push button)
    this group can populate to n arbitrary number of times base on user requirements.

    event involved is just the push button clicked event but I will have to get the text field of both the label and combo box in order to produce the output I want.

    the problem in the code below is all button will only display the last group items (label and combo box) but not their own group item which is not a surprise as the pointers will eventually point to the last set.

    How can i associate the groups together? Any input would be good.

    P.S. I initially though of array of pointers but it will kept my amount of group I can have. or waste some mem from unused pointers.

    disp.h
    Qt Code:
    1. ...
    2. private slots:
    3. void go_clicked();
    4.  
    5. private:
    6. void createPlace();
    7. QWidget *createCellWidget(group grp);
    8.  
    9. mapinfo map; <-- struct
    10.  
    11. QToolBox *toolbox;
    12. QLabel *grp;
    13. QComboBox *combo
    To copy to clipboard, switch view to plain text mode 

    disp.cpp
    Qt Code:
    1. ...
    2. void disp::createPlace()
    3. {
    4. int i = 0;
    5. QGridLayout *layout = new QGridLayout;
    6.  
    7. foreach (group grp, map.grp) {
    8. layout->addWidget(createCellWidget(grp), i, 0);
    9. i++;
    10. }
    11.  
    12. layout->setRowStretch(i, 10);
    13.  
    14. QWidget *itemWidget = new QWidget;
    15. itemWidget->setLayout(layout);
    16.  
    17. toolbox = new QToolBox;
    18. toolbox->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Ignored);
    19. toolbox->setMinimumWidth(itemWidget->sizeHint().width());
    20. toolbox->addItem(itemWidget,"Select Place");
    21. }
    22.  
    23. QWidget *disp::createCellWidget(group grp)
    24. {
    25. combo = new QComboBox;
    26. foreach (place p, grp.building)
    27. combo->addItem(p.placename);
    28.  
    29. combo->setMaximumWidth(200);
    30.  
    31. btn->setText("Go");
    32. btn->setMaximumWidth(50);
    33.  
    34. grp = new QLabel;
    35. grpname->setText(grp.grpname);
    36.  
    37. QGridLayout *layout = new QGridLayout;
    38. layout->addWidget(grpname, 0, 0);
    39. layout->addWidget(combo, 1, 0);
    40. layout->addWidget(btn,1,1);
    41.  
    42. QWidget *widget = new QWidget;
    43. widget->setLayout(layout);
    44.  
    45. connect(btn, SIGNAL(clicked()), this, SLOT(go_clicked()));
    46.  
    47. return widget;
    48. }
    49.  
    50. void disp::go_clicked()
    51. {
    52. //something with own group's combo text and label text
    53. qDebug() << grpname->text() << " ----- " << combo->currentText(); //testing
    54. }
    To copy to clipboard, switch view to plain text mode 

  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: need help in getting a logic to work

    It very well may be that I didn't understand your post.


    But one way to do it is by creating a simple custom widget something like
    Qt Code:
    1. class InfoWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. InfoWidget(const group &grp); //in the implementations you set the member widgets in the layout as you did in createCellWidget()
    6. ~InfoWidget ();
    7. public slots:
    8. void go_clicked();
    9. protected:
    10. QComboBox m_comBox;
    11. QLabel m_label;
    12. QToolBox m_toolBox;
    13. QPushButton m_button;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. InfoWidget::InfoWidget(const group &grp)
    2. {
    3. foreach (place p, grp.building)
    4. m_comBox.addItem(p.placename);
    5.  
    6. m_comBox->setMaximumWidth(200);
    7.  
    8.  
    9. m_button.setText("Go");
    10. m_button.setMaximumWidth(50);
    11.  
    12. m_label.setText(grp.grpname);
    13.  
    14. QGridLayout *layout = new QGridLayout();
    15. layout->addWidget(&m_label, 0, 0);
    16. layout->addWidget(&m_combo, 1, 0);
    17. layout->addWidget(&m_button,1,1);
    18.  
    19. setLayout(layout);
    20.  
    21.  
    22. connect(&m_button, SIGNAL(clicked()), this, SLOT(go_clicked()));
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    now you can access all the elements in the slot:
    Qt Code:
    1. InfoWidget::go_clicked()
    2. {
    3. //m_button, m_comBox, m_label are all members and can be accessed.
    4. }
    To copy to clipboard, switch view to plain text mode 
    and then you can do something like:
    Qt Code:
    1. QWidget *disp::createCellWidget(group grp)
    2. {
    3. // QVector<InfoWidget*> m_infoWidgets; in the header
    4.  
    5. InfoWidget *pInfoWidget = new InfoWidget(group);
    6. m_infoWidgets.push_back(pInfoWidget);
    7.  
    8. return infoWidget;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Please note that this is more like pseudo code, I did not test for compilation, it more to illustrate the idea I am suggesting.
    Last edited by high_flyer; 28th January 2010 at 09:31.
    ==========================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
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: need help in getting a logic to work

    thanks. it works. but I did not use QVector as there is a conversion problem. Instead I just use the customWidget as it is.

  4. #4
    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: need help in getting a logic to work

    QVector as there is a conversion problem.
    There should not be such a problem.
    Can you post the code that gives you that error?

    The vector allows you to have multiples such InfoWidgets.

    Also, what I forgot to mention in my previous post, you need to make sure you clear up (destroy) the allocated custom widgets in the destructor.
    ==========================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.

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2009, 20:27
  2. Ladder logic editor
    By GuL in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 22:02
  3. QSQLITE logic error
    By cyberboy in forum Qt Programming
    Replies: 11
    Last Post: 10th February 2008, 19:52
  4. Replies: 3
    Last Post: 15th January 2008, 12:11
  5. GL Logic op blending inside QGLWidget
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2006, 19:55

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.