Strange questions about QTableWidget
-
Hello, everyone
I used QTableWidget on my GUI. I add some QWidget(eg. QLabel) on the cell by setCellWidget, and set tooltip for the widget. Usually UI work well, but the UI crashed occasionally . some code like this:
@ //in myTabelWidget.h
MyTableWidget : QTableWidget(QWidget *pWidget)
{public:
void setToolTips();
...
}//in mainwindow.cpp construct function
MyTableWidget *pTableWidget = new MyTableWidget(this);
....//in myTableWidget.cpp construct function
QLabel *pLabel = new QLabel(this);
qTableWidget.setCellWidget(0, 1, pLabel);
...
//in myTableWidget::setToolTips()QWidget *pWidget = this->cellWidget(0, 1);
if(pWidget)
{
pWidget->setToolTip(QCoreApplication::translate("DynamicTranslate",
"TestToolTip"));// UI crash here
}
...
@I use QT debugger to attach the running process, and find the app crash setToolTip. I watched the stack data. I found the pWidget nonzero, but *pWidget is "", and internal data: QObject is "" , children has 0 items etc.
I confused the error what happened. If the QWidget not exist, call cellWidget will return zero. And the wonder is the pWidget not zero, but internal is null("")
Thanks for your help
-
Maybe bug? It's strange, that's true...we need here some Troll :)
-
Hi yeaiping,
can you post some more code for this?
What looks strange to me is this:
@
QLabel *pLabel = new QLabel(this);
qTableWidget.setCellWidget(0, 1, pLabel);
@what is qTableWidget???
And have you tried to debug the whole stuff?
-
The code posted by yeaiping has this line (13):
@
MyTableWidget *pTableWidget = new MyTableWidget(this);
@-That seems clear enough?-
Indeed, not clear. That pTableWidget was defined in MainWindow, while setTooltips is in MyTableWidget.I guess unrelated, but I think there is an error earlier on. Line 3 onwards should probably be:
@
class MyTableWidget : public QTableWidget {
Q_OBJECT
public:
MyTableWidget(QWidget *parent);
void setToolTips();//...
}
@ -
[quote author="Andre" date="1305731702"]The code posted by yeaiping has this line (13):
@
MyTableWidget *pTableWidget = new MyTableWidget(this);
@That seems clear enough?
Indeed, not clear. That pTableWidget was defined in MainWindow, while setTooltips is in MyTableWidget.
[/quote]But he stated the code is in
@
//in myTableWidget.cpp construct function
@which looks like the code for the class to me.
-
[quote author="Gerolf" date="1305731898"][quote author="Andre" date="1305731702"]The code posted by yeaiping has this line (13):
@
MyTableWidget *pTableWidget = new MyTableWidget(this);
@That seems clear enough?
Indeed, not clear. That pTableWidget was defined in MainWindow, while setTooltips is in MyTableWidget.
[/quote]But he stated the code is in
@
//in myTableWidget.cpp construct function
@which looks like the code for the class to me.
[/quote]
Yes, I agree, it is unclear. I was a bit confused by the unclear boundaries between different sections of code that was posted.@yeaiping:
Because there are already references to line numbers in the topic now, I won't change your opening post. But could you, next time, please make different code blocks for different files or classes, so it is immediately obvious what belongs together and what not? -
From your description it sounds like the label has been deleted behind the table's back. The memory has become invalid at some point since your application crashes when accessing it.
I believe any function call you call on the label at that point will make your application crash. Does it make a difference if you replace the setToolTip() call with another one, e.g setObjectName(). What happens if you remove the call alltogether, does the application run? And if so, is the label visible at all? Do you modify the label anywhere else in your code?
Also, when looking at the following piece of code you posted:
@//in myTableWidget.cpp construct function
QLabel *pLabel = new QLabel(this);
qTableWidget.setCellWidget(0, 1, pLabel);
...@then it looks like qTableWidget is created on the stack. You need to ensure it is created on the heap, otherwise qTableWidget will be deleted when going out of scope.
-
Thanks, all
And I'm sorry that my code / description too terrible to confuse everyone
This crash sometimes, I think maybe I delete the object before I used this, but I check my code, not found free object resource code. And I used Log4QT module to record the code's action, when the GUI crash, some logs lose, and not found crash context. And I try to use qt debugger to attach the crash app, and debugger point to the setToolTip. Maybe is debugger's fault, the crash posittion maybe is wrong.
And the release deploy don't crash by now.
-
Hi yeaiping,
As Andre pointed out earlier, I hope you have put the Q_OBJECT declaration in your MyTableWidget.h class declaration. Also, for objects inheriting from QObject, we can simply use tr("<your string>") to translate.
-
[quote author="jim_kaiser" date="1307907850"]Hi yeaiping,
As Andre pointed out earlier, I hope you have put the Q_OBJECT declaration in your MyTableWidget.h class declaration. Also, for objects inheriting from QObject, we can simply use tr("<your string>") to translate.[/quote]
Thanks