Results 1 to 13 of 13

Thread: Grid of QComboBox

Threaded View

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

    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.

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.