Connect external widget(slider, linedit) to QTableWidgetItem
I am trying to connect external widgets(slider, combobox, lineedit, etc) to specific QTableWidgetItem.
The widgets are not embed into the QTableWidget.
All the widgets have their own Signals, which is really fantastic,
BUT the QTableWidgetItem have no SLOTS!
How can i set the values of the QTableWidgetItems using other widgets outside the QTableWidget?
I tried something like the code below but it simply does not work
Code:
//setting up the table
dataSheet->setRowCount(3);
dataSheet->setColumnCount(7);
//initializing the QTableWidgetItem
int row = 0, col = 0;
dataSheet->setItem(row, col, newItem);
//creating external widget
//trying to connect?!
connect(action,
SIGNAL( textChanged
( const QString & ) ), newItem,
SLOT( setText
( const QString & ) ));
any suggestions?!
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Quote:
BUT the QTableWidgetItem have no SLOTS!
That's because QTableWidgetItem is not a QObject.
Quote:
How can i set the values of the QTableWidgetItems using other widgets outside the QTableWidget?
You can make a slot in your table widget, connect to that slot, and in it set the data to the items you want.
But it would better if you understand how Model/View works, to deal with data correctly through the model.
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Quote:
You can make a slot in your table widget, connect to that slot, and in it set the data to the items you want.
QTableWidget and the other widgets(slider, lineedit, etc) belong to one-central widget MyWidget.
do you mean to overload the QTableWidget class inserting a custom SLOT or to just add a SLOT to MyWidget?
Re: Connect external widget(slider, linedit) to QTableWidgetItem
i created this (doesn't work)
Code:
{
Q_OBJECT
public:
public Q_SLOTS:
};
when I remove the Q_OBJECT I cannot add a public Q_SLOTS
When I add the Q_OBJECT I get several errors like
Code:
/usr
/include
/qt4
/QtCore
/qobject.
h:296: error
: ‘QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData>
QObject::d_ptr’ is
protected
The idea is mainly to overload the QTableWidgetItem in order to add slots.
Am i completely wrong?
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Quote:
when I remove the Q_OBJECT I cannot add a public Q_SLOTS
When I add the Q_OBJECT I get several errors like
Again, QTableWidgetItem is NOT a QObject.
Only QObject can use the Q_OBJECT macro, and use signals/ slots.
Re: Connect external widget(slider, linedit) to QTableWidgetItem
As high_flyer said, QTableWidgetItem is not a QObject, if your class inherits only QTreeWidgetItem it wont be a QObject either.
You need to subclass QObject as well:
Re: Connect external widget(slider, linedit) to QTableWidgetItem
thank you very much! It worked indeed:
MyTableItem.h
Code:
{
Q_OBJECT
public:
public slots:
};
myTableItem.cpp
Code:
#include <myTableItem.h>
MyTableItem
::MyTableItem (const QString & text
){
setText(text);
}
void MyTableItem
::setTextSlot(const QString & t
) {
setText(t);
}
in the main
Code:
MyTable *dataSheet = new MyTable;//i have overloaded the QTableWidget too
MyTableItem *test = new MyTableItem("test");
dataSheet->setItem(2, 2, test);
connect(action,
SIGNAL( textChanged
( const QString & ) ), test,
SLOT( setTextSlot
( const QString & ) ));
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Now, I have a slightly different problem...
I want the action QLineEdit to set the values of a specific cell of the MyTable dataSheet (it's an overloaded QTableWidget):
Code:
MyTable *dataSheet = new MyTable;//i have overloaded the QTableWidget too
MyTableItem *test = new MyTableItem("test");
dataSheet->setItem(2, 2, test);
connect(action,
SIGNAL( textChanged
( const QString & ) ),dataSheet
->item
(2,
2) ,
SLOT( setTextSlot
( const QString & ) ));
I get the following error:
Code:
error
: no matching function
for call to ‘MyWidget
::connect(MyLineEdit
*&,
const char*,
QTableWidgetItem*,
const char*)’
/usr
/include
/qt4
/QtCore
/qobject.
h:198: candidates are
: static bool QObject::connect(const QObject*,
const char*,
const QObject*,
const char*, Qt
::ConnectionType) /usr
/include
/qt4
/QtCore
/qobject.
h:313: bool QObject::connect(const QObject*,
const char*,
const char*, Qt
::ConnectionType) const
I think I am close to make it work, but there's something wrong.
From the moment that this line works:
Code:
connect(action,
SIGNAL( textChanged
( const QString & ) ), test,
SLOT( setTextSlot
( const QString & ) ));
there must be a way to make this line work too:
Code:
connect(action,
SIGNAL( textChanged
( const QString & ) ),dataSheet
->item
(2,
2) ,
SLOT( setTextSlot
( const QString & ) ));
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Probably you haven't implemented the "item" method in your QTreeWidget subclass to return your custom items, so "item" still returns QTreeWidgetItem*, which is not a QObject and thus cannot be used in signal / slot mechanism.
If you want to go this way, one of solutions is - implement ' MyTable::item(int,int) ' to return ' MyTableItem* '.
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Did you mean this?
Code:
MyTableItem* MyTable::item ( int row, int column ) const
{
return item ( row, column );
}
It compiles fine, but I get segmentation fault because of this :(
the source code has something like this:
Code:
return tableItems.value(tableIndex(row, column));
but i don't know how to transform it to my class
Re: Connect external widget(slider, linedit) to QTableWidgetItem
Quote:
segmentation fault because of this
Yes, this is correct behaviour according to the implementation:)
Try to imagine what happens when you call
...
( small hint: item(2,2) => { return item(2,2) => { return item(2,2) => ... } } )
Calling the same method with the same arguments over and over...
What you want to do is to call base class implementation of "item" from your class "item" method and convert the result to your datatype. So there are two more questions:
1) how to call base class implementation from derived class method ?
2) how to convert from base class pointer to derived class pointer at runtime ?
Can't help you more with this, because next step is to write ~30 more characters in your "item" implementation:p Believe me, you'll learn much more by doing this yourself ( there is great, free C++ book available online called "Thinking in C++" )