Results 1 to 3 of 3

Thread: Creating signals in subclasses

  1. #1
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Creating signals in subclasses

    Hi everyone, I was hoping someone could help me out with this fairly simple (I hope) problem.

    I have created a subclass of the QComboBox, I want to change the combo box index based on the value stored in its associated userData. I essentially want to create a signal call currentIndexChanged(QVariant) (or in my case I am using int anyway). How do I cause my subclassed QComboBox to emit this signal when the currentIndexChanged(int) signal is emitted?

    I thought I should stick this connect in the header, but I seem to be having some problems...

    Qt Code:
    1. ComboBoxCopy::ComboBoxCopy(QWidget *parent)
    2. : QComboBox(parent)
    3. {
    4. connect(this,SIGNAL(currentIndexChanged(int)),this,SIGNAL(currentIndexChanged(QVariant)));
    5. }
    To copy to clipboard, switch view to plain text mode 

    or I'd be happy to rename the signal to prevent any possible confusing, at the moment I'm trying this.

    Qt Code:
    1. connect(this,SIGNAL(currentIndexChanged(int)),this,SIGNAL(currentIndexChangedData(int)));
    To copy to clipboard, switch view to plain text mode 

    I guess what I'm asking is, is this the right way to set this up?

    I appreciate any help, thanks!
    Michael

    Edit: Ok, so I realised that the parameters passed between these signals are obviously not interchangeable (one being an index and the other being a data value). I have now reimplemented the setCurrentIndex(int) slot from the QComboBox as follows, adding the required signal emission

    Qt Code:
    1. void ComboBoxCopy::setCurrentIndex(int index)
    2. {
    3. QComboBox::setCurrentIndex(index);
    4. int userdata = itemData(index, Qt::UserRole).toInt();
    5. emit currentIndexChangedData(userdata);
    6. }
    7.  
    8.  
    9. void ComboBoxCopy::setCurrentIndexFromData(int userdata)
    10. {
    11. int index = findData(userdata,Qt::UserRole,Qt::MatchExactly);
    12. if(index != -1){
    13. QComboBox::setCurrentIndex(index);
    14. emit currentIndexChangedData(userdata);
    15. }else{
    16. //error!
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    I still value any opinions on this, despite my monologuing here
    - Michael

    Edit2: Above code was obviously not effective, as I thought it was. Damn my premature responses to my own post. This following code seems to make the most simple solution. I've just posted it here so, in the case that it helps someone else. That said there is a good chance I'll find a problem with this too...

    Qt Code:
    1. ComboBoxCopy::ComboBoxCopy(QWidget *parent)
    2. : QComboBox(parent)
    3. {
    4. connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(emitDatafromIndex(int)));
    5. }
    6.  
    7. void ComboBoxCopy::emitDatafromIndex(int index)
    8. {
    9. int userdata = itemData(index, Qt::UserRole).toInt();
    10. emit currentIndexChangedData(userdata);
    11. }
    12.  
    13. void ComboBoxCopy::setCurrentIndexFromData(int userdata)
    14. {
    15. int index = findData(userdata,Qt::UserRole,Qt::MatchExactly);
    16. if(index != -1){
    17. QComboBox::setCurrentIndex(index);;
    18. }else{
    19. //error!
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cotlone; 21st June 2010 at 02:23. Reason: updated contents

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating signals in subclasses

    The last snippet is what I would use. However, I would call the signal something like void currentDataChanged(QVariant) (currentIndexChangedData() just sounds odd from a natural language point of view) and I would probably keep the QVariant in the signature as well.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating signals in subclasses

    Cool, thanks for your input. I didn't like the name either, though I had previously thought about your suggestion and from a natural language point of view it seems to imply that the data has changed, when it is actually the index. I guess a good way to do it would be to just call it currentIndexChanged(QVariant), which would be distinguishable from the QComboBox's signal currentIndexChanged(int).

Similar Threads

  1. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  2. Error creating subclasses
    By agerlach in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2010, 13:49
  3. declaring subclasses in C++
    By Paat in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 08:40
  4. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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.