Results 1 to 18 of 18

Thread: Model/View Programming -- Signals

  1. #1
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Model/View Programming -- Signals

    Hi,

    I am working with a QTableView with a custom delegate. In the createEditor function for the delegate I either create QLineEdit or QComboBox items. Can I connect to the signals that these items are emiting when they are used?

    Example: The QComboBox emits activated ( int index ) when an item in the QComboBox is activated.

    Do I have to subclass QTableView, connect the wanted signal from the returned widget from widget* createEditor? or can someone recommend another better approach?
    Last edited by KjellKod; 4th February 2006 at 15:36.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model/View Programming -- Signals

    You can use a QtableView without subclassing.

    set the delegate of your view using setItemDelegate(yourDelegate)
    Connections to your signals are already set up now.

    Cheers

  3. #3
    Join Date
    Jan 2006
    Location
    Minsk, Brest, Belarus
    Posts
    54
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Quote Originally Posted by KjellKod
    Hi,

    I am working with a QTableView with a custom delegate. In the createEditor function for the delegate I either create QLineEdit or QComboBox items. Can I connect to the signals that these items are emiting when they are used?

    Example: The QComboBox emits activated ( int index ) when an item in the QComboBox is activated.

    Do I have to subclass QTableView, connect the wanted signal from the returned widget from widget* createEditor? or can someone recommend another better approach?
    Can you show the code please?
    I'm very interested in code like this.
    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model/View Programming -- Signals

    something like :

    QTableView* myView;
    myView->setItemDelegate(yourDelegate);

    The delegate has signals like closeEditor, that is connected to the view's closeEditor Slot automatically.
    If you want custom slots then you'll have to make the connections yourself.

    What code do you have already, maybe we can fill in the gaps.


    Cheers

  5. #5
    Join Date
    Jan 2006
    Location
    Minsk, Brest, Belarus
    Posts
    54
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    I know how to realize delegating items.
    I wanted to see the code where the delegate item is QTextEdit and when user presses Enter button he can continue editing text.

    Sorry, for my English.

  6. #6
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Hi again.

    The problem is not in creating the delegate. I've one and it works. The problem is that that in the QTableView I use items, sometimes QLineEdit and sometimes QComboBox items that are created in the delegate. The issue is really that I can't seem to getI the QLineEdit's and QComboBox's specific signals that are emitted when the user is doing things in them (selecting item, etc).

    Code example: Sorry if I'm writing this, maybe it's better to include files?
    Qt Code:
    1. UIQtCookBook::setupItemViews()
    2. {
    3. /* Other code here... setting the Model for the view etc */
    4. // Setting the delegate for the view
    5. IngredientTableDelegate *ingredientTableDelegate = new IngredientTableDelegate();
    6. ingredientsTableView->setItemDelegate( ingredientTableDelegate);
    7. }
    8.  
    9. // Other file.
    10. // IngredientTableDelegate is subclass of QItemDelegate
    11. //
    12. QWidget* IngredientTableDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index ) const
    13. {
    14. QWidget* editor = 0;
    15.  
    16. if( checkIfCombo() )
    17. {
    18. editor = new QComboBox (parent);
    19. const QAbstractItemModel* model = index.model();
    20. QVariant value = model->data( index, Qt::DisplayRole );
    21.  
    22. // Add the list of measurements to the QComboBox
    23. QString measurement= value.toString();
    24. QComboBox* box = new QComboBox( parent );
    25. box->addItems( m_measurements );
    26. editor = box;
    27. // The connection below does not work
    28. connect(box, SIGNAL(activated(int)), this, SLOT(setTestPrintout(int)));
    29. }
    30. else
    31. {
    32. editor = new QLineEdit( "-na-", parent);
    33. const QAbstractItemModel* model = index.model();
    34. QVariant value = model->data( index, Qt::DisplayRole );
    35. QLineEdit* editLine = new QLineEdit(parent);
    36. editor = editLine;
    37. }
    38.  
    39. editor->installEventFilter(const_cast<IngredientTableDelegate*>(this));
    40. return editor;
    41. }
    To copy to clipboard, switch view to plain text mode 

    So far so good. But what I really what to do is to catch the activated signal from QComboBox items, basically I wan't to call a specific function when a certain value in the QComboBox is chosen. I do not seem to be able to do that.

    I.e. the bottom line is. How can I connect the signals from the dynamically created QWidgets in createEditor above to another function in this class. I've tried (naive approach?) to do it in the createEditor function, but I do not get the expected result (see the comment in the code --- the printout function is never called)
    Last edited by KjellKod; 4th February 2006 at 19:57.

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model/View Programming -- Signals

    when user presses Enter button he can continue editing text.
    Do you mean you want to go to the next cell when pressing Enter?

    If that's the case you'll have to subclass the delegates eventFilter. When you press enter you emit the closEditor signal and use the EditNextItem flag. This will open the next cell for editing.
    Qt Code:
    1. bool autoSqlRelationalDelegate::eventFilter(QObject *object, QEvent *event)
    2. {
    3. QWidget *editor = ::qobject_cast<QWidget*>(object);
    4. if (!editor)
    5. return false;
    6. if (event->type() == QEvent::KeyPress) {
    7. switch (static_cast<QKeyEvent *>(event)->key()) {
    8. case Qt::Key_Tab:
    9. emit commitData(editor);
    10. emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
    11. return true;
    12. case Qt::Key_Backtab:
    13. emit commitData(editor);
    14. emit closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
    15. return true;
    16. case Qt::Key_Enter:
    17. emit commitData(editor);
    18. emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
    19. case Qt::Key_Return:
    20. emit commitData(editor);
    21. emit closeEditor(editor,delegate::EditNextItem);
    22. return true;;
    23. ...
    To copy to clipboard, switch view to plain text mode 

    Cheers

  8. The following user says thank you to Everall for this useful post:

    jml (23rd July 2007)

  9. #8
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Sorry guys, but Xagen it's a bit confusing having different questions in the same thread!!! Sorry for the rebuke. But maybe it's easier to have two threads since we're not trying to achieve the same thing or?
    Last edited by KjellKod; 4th February 2006 at 20:11.

  10. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model/View Programming -- Signals

    sorry KjellKod I didn't notice the questions came from 2 different people.
    And yes it's a little confusing.

    I'll lhave a look into your code.

    cheers,

    Everall

  11. #10
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Thanks Everall -- I'll be away for a few hours but I'll look back into this forum later. Any help is most appreciated

  12. #11
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Hmmm. I found a Trolltech example that is almost identical to what I'm doing.
    http://doc.trolltech.com/4.1/widgets-icons.html
    --- I'll read through that example and see what I'm missing in the picture

  13. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Model/View Programming -- Signals

    But what I really what to do is to catch the activated signal from QComboBox items, basically I wan't to call a specific function when a certain value in the QComboBox is chosen. I do not seem to be able to do that.
    Here is an idea:
    If you choose an item in the combobox then the value of the cell is set in the view.

    the view emits the activated(QmodelIndex& index) signal. You can connect it with the views datachanged(index, index) slot in the constructor of the view.

    When you reimplement the datachanged slot you could determine where the signal comes from with sender() to determine if the signal comes from the combobox.

    Hope this helps

  14. #13
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals [SOLVED]

    Problem solved!
    Thanks to that I'm a little rusty with Qt programming I guess and that the signal slot mechanism is very similar to the signal mechanism we use at work I got a little confused. Thanks for the great help though it was super to get some feedback and it did put me on the right track by just giving me the reason to look elsewhere for the solution.

    Oh, and what the solution was. Heh, almost ashamed to say it, but I had not declared
    the setTestPrintout(int))); as a slot.

    I.e I changed:
    Qt Code:
    1. void setEditable2(int test);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. private slots:
    2. void setEditable2(int test);
    To copy to clipboard, switch view to plain text mode 
    Last edited by KjellKod; 4th February 2006 at 23:38. Reason: The problem is solved

  15. #14
    Join Date
    Jan 2006
    Location
    Minsk, Brest, Belarus
    Posts
    54
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals

    Quote Originally Posted by KjellKod
    Sorry guys, but Xagen it's a bit confusing having different questions in the same thread!!! Sorry for the rebuke. But maybe it's easier to have two threads since we're not trying to achieve the same thing or?
    Sorry! I wasn't attentive....

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View Programming -- Signals [SOLVED]

    Quote Originally Posted by KjellKod
    Oh, and what the solution was. Heh, almost ashamed to say it, but I had not declared
    the setTestPrintout(int))); as a slot.
    That's why it is good to look at the console while writing the code. Qt surely warned you that such a slot doesn't exist.

    Great that you managed to go through with it.

  17. #16
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals [SOLVED]

    Quote Originally Posted by wysota
    That's why it is good to look at the console while writing the code. Qt surely warned you that such a slot doesn't exist.
    Nope, no such thing. I had to re-make my error and re-compile & re-run the program just to test it to see if you were right, but no. Too bad it didn't have that though.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals [SOLVED]

    Quote Originally Posted by KjellKod
    I had to re-make my error and re-compile & re-run the program just to test it to see if you were right, but no.
    Add "CONFIG += console" to your .pro file and try again.

  19. #18
    Join Date
    Jan 2006
    Location
    Scandinavia
    Posts
    62
    Thanks
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View Programming -- Signals [SOLVED]

    Quote Originally Posted by jacek
    Add "CONFIG += console" to your .pro file and try again.
    Jacek thank you that worked great.
    With Qt configured in debug mode and then re-generating Makefile, re-compiling and re-running the program I managed to trigger the wanted warning message:

    Object::connect: Connecting from COMPAT signal (QTabWidget::currentChanged(QWidget*)).
    createEditor called
    Object::connect: No such slot IngredientTableDelegate::setEditable2(int)
    I'll make sure that I always keep doing this with debug and console enabled
    Muchas gracias

Similar Threads

  1. questions about datachanged() in model/view programming
    By calmspeaker in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 23:48
  2. A question about Model/View programming
    By awhite1159 in forum Qt Programming
    Replies: 5
    Last Post: 15th June 2008, 15:38
  3. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  4. MODEL/VIEW programming and TABLES
    By newbie in forum Qt Programming
    Replies: 5
    Last Post: 27th August 2006, 21:26
  5. MODEL/VIEW programming
    By mira in forum Newbie
    Replies: 3
    Last Post: 21st April 2006, 11:19

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.