Results 1 to 4 of 4

Thread: Item Delegate - Relay a signal to editor widget

  1. #1
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Item Delegate - Relay a signal to editor widget

    Hello - I know this *should* work, but I can't seem to figure it out. I have a QTreeView with a complex ItemDelegate that is made up of several widgets inside a frame. I want to insert some text(the name of a database field) into one of those widgets on a double-click in another TreeWidget ( a list of database fields in a docking window). I have done this:

    connected the double-click signal from the docking window TreeWidget to the ItemDelegate:
    Qt Code:
    1. connect( this->getFieldsList(), SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int) ), del, SLOT( insertText( QTreeWidgetItem *, int ) ) );
    To copy to clipboard, switch view to plain text mode 
    where del is the item delegate. This works fine.

    defined a slot in the item delegate called insertText as:
    Qt Code:
    1. void RWDelegate::insertText( QTreeWidgetItem *item, int col )
    2. {
    3. QString qualified_name = item->parent()->text( col ) +"."+ item->text( col );
    4. int cnt = receivers( SIGNAL(textToInsert( const QString & ) ) );
    5. emit textToInsert( qualified_name );
    6. }
    To copy to clipboard, switch view to plain text mode 
    the number of receivers is always 0, by the way

    defined a signal in the item delegate as:
    Qt Code:
    1. signals:
    2. void textToInsert( const QString & qualified_name );
    To copy to clipboard, switch view to plain text mode 

    connected the specific widget that should receive the textToInsert signal in createEditor as:
    Qt Code:
    1. RWFilterEdit *query_filter = new RWFilterEdit( query_open );
    2. bool isconnect = connect( this, SIGNAL( textToInsert( const QString &)), query_filter , SLOT( Inserter( const QString &) ) );
    To copy to clipboard, switch view to plain text mode 
    the variable isconnect returns true

    defined a slot in the item delegate widget class (RWFilterEdit) as:
    Qt Code:
    1. public slots:
    2. void Inserter( const QString & qualified_name );
    To copy to clipboard, switch view to plain text mode 

    I can see the signal get emitted when debugging... but it never hits a break point in the Inserter slot. I'm processing some other signals from other widgets - but they're Qt signals (eg. currentIndexChanged) and they work fine. What am I missing???

    Oh.. also a follow on question -- I hope this isn't a silly one -- why does Intellisense show the QString class as type" static int RWDelegate::QString" in my idem delegate? Other Qt classes are shown as (example) "class QTreeView"? Just curious.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Item Delegate - Relay a signal to editor widget

    Not an answer to your question but a personal opinion (if you don't mind)

    In my opinion, the only objects that need and can access a delegate are the connected models and views, nothing else.
    If you want to get data from a database, then this should be done inside the delegate itself, based on the model index and other data (like roles etc...)

    I think you have it backwards. You click in a treeview and then instruct a delegate to insert some text?
    This treeview (or widget) is another one than the one where you actually use the delegate? If so, then clicking in the first treeview should set some data on the model of the second treeview, and then use the delegate to react on this datachange.

  3. #3
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Item Delegate - Relay a signal to editor widget

    I never mind opinions! The end user is building an SQL Query. So, they are typing text into a QEditField. The external tree view displays a list of available database fields for the convenience of properly constructing the query. I don't mind pushing the data to the model instead and it does seem that the fundamental architecture of Qt is not compatible with this approach...but wouldn't that mean closing the current editor with unfinished data, inserting the selected data from the external tree (though, you'd have to append it, because you wouldn't have access to the cursor position after the delegate closed), and then re-init the delegate with the new information? Is that right, or is there another way to do it?

  4. #4
    Join Date
    Nov 2009
    Posts
    44
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Item Delegate - Relay a signal to editor widget

    In case anyone else needs to do something like this... Here is how I solved it. It's not pretty, but it works. In the RWItemDelegate::insertText slot above, I put the following code:
    Qt Code:
    1. QTreeView* theview = ((QtRW*)sender()->parent()->parent()->parent() )->findChild<QTreeView*>("view");
    2. QModelIndex index = theview->selectionModel()->currentIndex();
    3.  
    4. QWidget* editor = theview->indexWidget( index );
    To copy to clipboard, switch view to plain text mode 
    once I have a pointer to the actual editor for the index, then I can find my QTextEdit field and the current cursor position and use the insertPlainText slot/function to insert the text. If anyone has a more elegant solution, I'd be more than happy to play with it.

Similar Threads

  1. Item Delegate Editor Size
    By QAlex in forum Qt Programming
    Replies: 0
    Last Post: 19th April 2010, 16:43
  2. Item Delegate editor background
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2010, 21:33
  3. Item Delegate and Signal/Slot
    By LynneV in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2010, 14:16
  4. Replies: 2
    Last Post: 12th February 2010, 04:41
  5. Replies: 5
    Last Post: 10th August 2009, 10:50

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.