Results 1 to 13 of 13

Thread: Grid of QComboBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

  5. #5
    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?

  6. #6
    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.

  7. #7
    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?

  8. #8
    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
  •  
Qt is a trademark of The Qt Company.