Results 1 to 10 of 10

Thread: Emit signal from const function

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Emit signal from const function

    Seems like this has been beat to death in past posts, but I can't seem to figure out how to apply a solution to my problem.

    I have a table view in a form built with designer. 2 columns have item delegates to provide combo boxes to limit the choices of entry values. The problem I have is in trying to limit the choices of the second column based on what is chosen from the combo box in the first column. The solution seemed to be to emit a signal from column 1 when the value was edited. Then receive the value in a slot in the column 2 delegate and use the received value to limit the combo box choices in column 2.

    Problem is that the setModelData function in column 1 won't work unless it is declared const. Then you can't emit a signal from it. In one post Wysota said, declare the slot const too. Tried that but it still gets the same compile error about discards qualifiers.

    Here is the relevant part of the code - column 1 delegate with signal:
    Qt Code:
    1. void ModeDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
    2. const QModelIndex& index) const
    3. {
    4. QComboBox* comboBox = static_cast<QComboBox*>(editor);
    5. QString value = comboBox->currentText();
    6.  
    7. qDebug() << "mode delegate data is " << value;
    8. emit modeChanged(value); // CAUSES THE COMPILE ERROR
    9. model->setData(index, value, Qt::EditRole);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Here is the slot in column 2 delegate:
    Qt Code:
    1. void FilterDelegate::getMode(QString value) const
    2. {
    3. qDebug() << "mode value received is " << value;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way around this, or another way I can get the column 1 selected value to column 2?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Emit signal from const function

    You could get the value from column 1 of your model in your column 2 delegate. Something like this:
    Qt Code:
    1. void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const //column 2 delegate
    2. {
    3. QString column_one_value = index.model()->data(index.model()->index(index.row(), index.column()-1));
    4. // then adjust your combobox model here
    To copy to clipboard, switch view to plain text mode 
    HTH

  3. The following user says thank you to norobro for this useful post:

    waynew (22nd August 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit signal from const function

    Or in a little bit simpler syntax
    Qt Code:
    1. QString columnOneValue = index.sibling(index.row, 1).value().toString();
    To copy to clipboard, switch view to plain text mode 

  5. The following 2 users say thank you to Lykurg for this useful post:

    norobro (22nd August 2010), waynew (22nd August 2010)

  6. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Emit signal from const function

    Is there a way around this (...)
    In general, you may try something like :
    Qt Code:
    1. const_cast< MyClass* >(this)->emit mySignal();
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to stampede for this useful post:

    waynew (22nd August 2010)

  8. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit signal from const function

    Quote Originally Posted by stampede View Post
    In general, you may try something like :
    Qt Code:
    1. const_cast< MyClass* >(this)->emit mySignal();
    To copy to clipboard, switch view to plain text mode 
    No we wont, since emit or Q_EMIT is a keyword/marco and not a member of a class.

  9. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit signal from const function

    Just for record, even if you don't need it in your case you can make the signal const. This works for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class test : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. void emitSignal() const
    9. {
    10. Q_EMIT sig("This is a test!");
    11. }
    12.  
    13. Q_SIGNALS:
    14. void sig(const QString&) const;
    15.  
    16. public Q_SLOTS:
    17. void slot(const QString& str) const
    18. {
    19. qWarning() << Q_FUNC_INFO << str;
    20. }
    21. };
    22.  
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. QApplication app(argc, argv);
    27.  
    28. test t;
    29. QObject::connect(&t, SIGNAL(sig(const QString&)), &t, SLOT(slot(const QString&)));
    30. t.emitSignal();
    31.  
    32. return 0;
    33. // return app.exec();
    34. }
    35.  
    36. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  10. #7
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Emit signal from const function

    Quote Originally Posted by Lykurg View Post
    No we wont, since emit or Q_EMIT is a keyword/marco and not a member of a class.
    but you can call const_cast< MyClass* >(this)->mySignal();
    emit is not necessary cause the SIGNALS are just method declarations.

    But if there is a better way, avoid to manipulate the constness with const_cast.

  11. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit signal from const function

    Quote Originally Posted by yakin View Post
    Yes, sorry, you are right! I was confused about the notation with the keyword.

  12. #9
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Emit signal from const function

    Thanks for your help folks. Got it working by reading the previous column with your suggestions.
    As usual, trying to make something too complicated!

  13. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Emit signal from const function

    emit is not necessary (...)
    Sure, I've just putted it there to show that this is still related to original signal question.

    But if there is a better way, avoid to manipulate the constness with const_cast.
    Of course, i agree 100%, I think a need for const_cast could be just a bad class desing. But C++ allows that. Personally, I will never persuade anyone to use it, but this is one of possible solutions for the problem.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. emitting signals in const function
    By maxel in forum Qt Programming
    Replies: 3
    Last Post: 19th October 2008, 15:05
  3. subclassed const function problem
    By qtneuling in forum Newbie
    Replies: 8
    Last Post: 22nd June 2008, 02:52
  4. const function parameter problems
    By stevey in forum General Programming
    Replies: 3
    Last Post: 18th December 2006, 22:22
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.