Results 1 to 13 of 13

Thread: Grid of QComboBox

  1. #1
    Join Date
    Aug 2012
    Location
    Paris
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Grid of QComboBox

    Dear Sirs,

    I'm a newcomer in the QT4 world, and I decided to use this wonderful tool for a software GUI.

    I would like to design a grid of ComboBox, to get input data. I succeded in designing it.
    However, all the boxes have the same name : ComboBox, and so I don't know how to get the data of a single comboBox (see code bellow).

    Could any one have an idea me to properly reach my goal ??

    Thanks in advance, and Best Regards,

    Stéphane (Alias Rakma74)

    Qt Code:
    1. // Data assigmnent window filling with input sub-widgets
    2. for (unsigned int row = 0; row < columns_number; row++)
    3. {
    4. Label = new QLabel(tr("Data ")+QString::number(row+1)+" :");
    5.  
    6. ComboBox = new QComboBox;
    7. ComboBox->addItem(tr("To be set..."));
    8. ComboBox->addItem(tr("A"));
    9. ComboBox->addItem(tr("B"));
    10. ComboBox->addItem(tr("C"));
    11. ComboBox->addItem(tr("D"));
    12.  
    13. grid->addWidget(Label, row, 0);
    14. grid->addWidget(ComboBox, row, 1);
    15. }
    16.  
    17. //
    18. data_assigment_input->setLayout(grid);
    19.  
    20.  
    21.  
    22. // Quit push button creation
    23. QPushButton *quit = new QPushButton(tr("OK"));
    24. // quit->setFont(QFont("Times", 18, QFont::Bold));
    25.  
    26. // Connection between widgets
    27. connect(quit, SIGNAL(clicked()), window_data_assignment, SLOT(close()));
    28.  
    29. // Size/Position auto-managment by QVBoxLayout
    30. QVBoxLayout *wlayout = new QVBoxLayout;
    31. wlayout->addWidget(data_assigment_input);
    32. wlayout->addWidget(quit);
    33. window_data_assignment->setLayout(wlayout);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grid of QComboBox

    Hi,

    You could just define an array of pointers to QComboBox objects. If the column_number is a constant during compilation, you can use simple array definition. If the column_number is a variable, then you need to use new [] operator. Make sure though you are going to release memory by using delete []comboBox at the ned of application. Otherwise your memory will be slowly leaking.

    Qt Code:
    1. // Data assigmnent window filling with input sub-widgets
    2. QComboBox* comboBox[column_number]; // if column_number if a constant
    3. // QComboBox** comboBox = new QComboBox*[column_number]; // this will have to be release by using delete []comboBox;
    4. for (unsigned int row = 0; row < columns_number; row++)
    5. {
    6. Label = new QLabel(tr("Data ")+QString::number(row+1)+" :");
    7.  
    8. comboBox[row]->addItem(tr("To be set..."));
    9. comboBox[row]->addItem(tr("A"));
    10. comboBox[row]->addItem(tr("B"));
    11. comboBox[row]->addItem(tr("C"));
    12. comboBox[row]->addItem(tr("D"));
    13.  
    14. grid->addWidget(Label, row, 0);
    15. grid->addWidget(comboBox[row], row, 1);
    16. }
    17.  
    18. //
    19. data_assigment_input->setLayout(grid);
    20.  
    21.  
    22.  
    23. // Quit push button creation
    24. QPushButton *quit = new QPushButton(tr("OK"));
    25. // quit->setFont(QFont("Times", 18, QFont::Bold));
    26.  
    27. // Connection between widgets
    28. connect(quit, SIGNAL(clicked()), window_data_assignment, SLOT(close()));
    29.  
    30. // Size/Position auto-managment by QVBoxLayout
    31. QVBoxLayout *wlayout = new QVBoxLayout;
    32. wlayout->addWidget(data_assigment_input);
    33. wlayout->addWidget(quit);
    34. window_data_assignment->setLayout(wlayout);
    To copy to clipboard, switch view to plain text mode 


    A side note, I usually use size_t, which is probably an equivalent of uint, as a counter. Also, when you define a variable, try to get used to start variable names with lower case letters, like thisFormOfName. Names beginning with a capital letter should be reserved for new classes, e.g. myNewObject of MyNewClass MyWindow, MyLabel etc. Also, the variable counter_number has different form of name: my_new_varialbe. It is also a good habit to stick to one form, either new_name or newName. It is easier to analyse a code. I also add suffix describing what variable represents QLabel, QPushButton etc. For instance, quitButton, vegComboBox, firstLabel, secondLabel, firstEdit, secondEdit (QEditLine). This helps to write code when Qt suggest names.
    Last edited by ZikO; 12th August 2012 at 06:23.

  3. #3
    Join Date
    Aug 2012
    Location
    Paris
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grid of QComboBox

    Thank you so much for your answer ZikO !! It works perfectly... (OMG, I lost half a day with this trouble...)

    Also, thank you very much for your advice... In fact, 'columns_number' could change during the execution, because it is the column number of the main Qtable of the application, and the ComboBox is used to setup each column, so I'm going to use your second initialisation !!

    Thanks for all !!

    Best Regards,

    Stéphane

  4. #4
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grid of QComboBox

    Np. I am glad it's working for you. You need to remember that the second choice requires to release memory and use delete []comboBox somewhere. If you don't do it, the memory will be leaking.

    What I helped with is a fundamental of C++. There are two great books that may help you to catch up. One of them is only about ISO/ANSI C++ not Qt but still useful as we use C++ in Qt. The link to a free online version is here:
    http://www.mindview.net/Books/TICPP/...gInCPP2e.html/
    It has been released in two volumes. First is about C++ basics whereas the second about Standard Template Library, exceptions etc. still useful things but they are substituted or not really required when using Qt as Qt provide its own versions (I think wow) and solutions.

    The second book was suggested in another forum about Qt (http://qt-project.org/forums/viewthread/19243/). It looks more like reference than real book but it can still be useful.
    http://www.ics.com/designpatterns/

  5. #5
    Join Date
    Aug 2012
    Location
    Paris
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grid of QComboBox

    Thanks so much for all the links you provided me !!

    That's sure that I really need to go deeper inside several C++ principles and concepts, to avoid to be limitated in my development !
    I'll read them...

    Thanks again !!

    Stéphane

  6. #6
    Join Date
    Aug 2012
    Location
    Paris
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grid of QComboBox

    Dear ZikO,

    I unfortunately have another problem...

    I successfully performed a connection between the comboBoxes, and the slot function, I'm only able to get the argument of the selected data, but not of the current ComboBox which has been clicked...

    for (unsigned int row = 0; row < columns_number; row++)
    connect(ComboBox[row], SIGNAL(currentIndexChanged(int)), this, SLOT(data_assign(int)));

    Would you have any tip ??

    Thanks in advance, and Best Regards !!

    Stéphane

  7. #7
    Join Date
    Aug 2012
    Location
    Paris
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Grid of QComboBox

    I finally succeded in getting the needed data in declaring --- QComboBox** comboBox --- in the MainWindow class, so that I can have access to its data in the slots...

    Thanks again...

    Stéphane

  8. #8
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grid of QComboBox

    Quote Originally Posted by Rakma74 View Post
    I successfully performed a connection between the comboBoxes, and the slot function, I'm only able to get the argument of the selected data, but not of the current ComboBox which has been clicked...

    for (unsigned int row = 0; row < columns_number; row++)
    connect(ComboBox[row], SIGNAL(currentIndexChanged(int)), this, SLOT(data_assign(int)));

    Would you have any tip ??
    I don't know how to identify an object other way than using a trick. There is one way to find out which object sent the signal. In your program, you have connected a couple of object signals into one slot. As the signal sends only index, you cannot really know which combo box sent the signal from signal/slot mechanism. This is tricky and I am not sure if it's proper technique but you may want to use QObject::sender() method that returns an address of a sender. Something like this:
    Qt Code:
    1. void YourClassName::data_assign(int item) {
    2. QComboBox* currentSender = static_cast<QComboBox*>(QObject::sender());
    3. std::size_t i = 0;
    4. while((i<rows) && (currentSender != comboBox[i])) {
    5. i++;
    6. }
    7. // "i" the index of your array and points out the sender which is one of the combo box
    8. }
    To copy to clipboard, switch view to plain text mode 

    The trick is that QObject::sender() provides an address but it is of a QObject type. Without casting it into QComboBox, compiler wouldn't accept it. Casting this address to QComboBox solves this problem here. However, I personally don't like it. It is very easy to mess up. I wouldn't try to do anything else then just comparing the addresses. In general, it's better to avoid things like this or connect object signals only of the same type to the same slot. Then, you can be certain of the type of a sender.

    Hope it helps.

    PS> I am still C++ programmer not Qt C++ >.< . Instead of using static_cast<QComboBox*> Qt provides qobject_cast<> which I believe is saver:
    Qt Code:
    1. QComboBox* currentSender = qobject_cast<QComboBox*>(QObject::sender());
    To copy to clipboard, switch view to plain text mode 
    Last edited by ZikO; 13th August 2012 at 03:51.

  9. #9
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Grid of QComboBox

    There are two alternatives to using QObject::sender() (the use of which is discouraged):

    1 (recommended). Use QSignalMapper. It can identify senders by an integer, a pointer, or a QString. Your slot will then be able to identify the QComboBox whose index changed, but will lose the argument index of currentIndexChanged(int index). This is not a problem since you can always get that value with QComboBox::currentIndex().

    2. Subclass QComboBox and add a custom signal with an additional parameter allowing to identify the combobox, e.g. MyComboBox::currentIndexChanged(int index, int id). In MyComboBox, connect QComboBox::currentIndexChanged(int index) to a private slot which emits your custom signal with the appropriate identifier.

  10. #10
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grid of QComboBox

    Quote Originally Posted by yeye_olive View Post
    2. Subclass QComboBox and add a custom signal with an additional parameter allowing to identify the combobox, e.g. MyComboBox::currentIndexChanged(int index, int id). In MyComboBox, connect QComboBox::currentIndexChanged(int index) to a private slot which emits your custom signal with the appropriate identifier.
    Hi yeye_olive,

    I am interested in this approach. Could you provide a simple code? I thought about that and would probably suggested it if I knew how to code custom signals / slots. Are signals also simple methods like slots?

  11. #11
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Grid of QComboBox

    Here we go for the second approach (subclass QComboBox). I recommend the first approach (QSignalMapper), though.

    Disclaimer: I did not even try to compile this code, there may be errors.

    Qt Code:
    1. class MyComboBox : public QComboBox {
    2. Q_OBJECT
    3. public:
    4. MyComboBox(int id, QWidget *parent = 0);
    5. signals:
    6. void currentIndexChanged(int index, int id);
    7. protected slots:
    8. void onCurrentIndexChanged(int index);
    9. protected:
    10. int m_id;
    11. };
    12.  
    13. MyComboBox::MyComboBox(int id, QWidget *parent) : QComboBox(parent), m_id(id) {
    14. connect(this, SIGNAL(currentIndexChanged(int)), SLOT(onCurrentIndexChanged(int)));
    15. }
    16.  
    17. void MyComboBox::onCurrentIndexChanged(int index) {
    18. emit currentIndexChanged(index, m_id);
    19. }
    To copy to clipboard, switch view to plain text mode 
    Signals are not simple methods (although they are implemented by moc as methods). They are emitted by a component and you need to connect them to a slot to react to their emission.

  12. #12
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Grid of QComboBox

    Thanks for this. I can see that your connect function has only three arguments whereas it should have 4: Widget, SIGNAL, Widget, SLOT ? Should there be this in the middle, as the third parameter?

  13. #13
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Grid of QComboBox

    That is because I used the QObject::connect() non-static method, which is indeed equivalent to using the static one with "this" between the signal and the slot parameters.

Similar Threads

  1. Better Grid
    By Buby in forum Newbie
    Replies: 2
    Last Post: 7th September 2010, 09:18
  2. Grid-like Toolbar
    By dv_ in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2009, 10:37
  3. drawing a grid
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2008, 09:17
  4. Grid Problem
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2007, 10:40
  5. How do I layout in a Grid?
    By DPinLV in forum Qt Tools
    Replies: 7
    Last Post: 10th August 2006, 01:37

Tags for this Thread

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.