PDA

View Full Version : Item Delegate - Relay a signal to editor widget



LynneV
25th June 2010, 18:36
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:

connect( this->getFieldsList(), SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int) ), del, SLOT( insertText( QTreeWidgetItem *, int ) ) );
where del is the item delegate. This works fine.

defined a slot in the item delegate called insertText as:

void RWDelegate::insertText( QTreeWidgetItem *item, int col )
{
QString qualified_name = item->parent()->text( col ) +"."+ item->text( col );
int cnt = receivers( SIGNAL(textToInsert( const QString & ) ) );
emit textToInsert( qualified_name );
}
the number of receivers is always 0, by the way

defined a signal in the item delegate as:

signals:
void textToInsert( const QString & qualified_name );

connected the specific widget that should receive the textToInsert signal in createEditor as:

RWFilterEdit *query_filter = new RWFilterEdit( query_open );
bool isconnect = connect( this, SIGNAL( textToInsert( const QString &)), query_filter , SLOT( Inserter( const QString &) ) );
the variable isconnect returns true

defined a slot in the item delegate widget class (RWFilterEdit) as:

public slots:
void Inserter( const QString & qualified_name );

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.

tbscope
25th June 2010, 18:50
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.

LynneV
25th June 2010, 20:21
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?

LynneV
5th July 2010, 16:36
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:

QTreeView* theview = ((QtRW*)sender()->parent()->parent()->parent() )->findChild<QTreeView*>("view");
QModelIndex index = theview->selectionModel()->currentIndex();

QWidget* editor = theview->indexWidget( index );
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.