QLabel->setText with access violation
-
Hi Community,
I would love to hear what are your assumptions to this error. I tried to overwrite a QLabel's text and I got the following error: "Access violation reading location 0xDDDDDF25".in my groupBoxWidget.h class there's this member pointer declared as private and a public Setter:
public: void SetInputMode(); private: QLabel *m_pLabelTitle1; //in the Class Constructor it is defined with a empty string: //m_pLabelTitle1 = new QLabel("");This method is in the groupBoxWidget.cpp defined as follows:
void CGroupBoxWidget::SetInputMode() { m_pLabelTitle1->setText(tr("Add files with extension:")); }First I tried to delete the "translation (tr)" and kept only the raw text between quotemarks. It didn't solve the problem. Then I tried to pass a constant QString by reference as:
const QString test = tr("Add files with extension:"); m_pLabelTitle1->setText(&test);And it didn't work. But then I deleted the ampersand (&) and it worked! I cannot see what is the error/conflict factor here.
I'm using Qt 5.8 with MSVC v15.
-
Hi Community,
I would love to hear what are your assumptions to this error. I tried to overwrite a QLabel's text and I got the following error: "Access violation reading location 0xDDDDDF25".in my groupBoxWidget.h class there's this member pointer declared as private and a public Setter:
public: void SetInputMode(); private: QLabel *m_pLabelTitle1; //in the Class Constructor it is defined with a empty string: //m_pLabelTitle1 = new QLabel("");This method is in the groupBoxWidget.cpp defined as follows:
void CGroupBoxWidget::SetInputMode() { m_pLabelTitle1->setText(tr("Add files with extension:")); }First I tried to delete the "translation (tr)" and kept only the raw text between quotemarks. It didn't solve the problem. Then I tried to pass a constant QString by reference as:
const QString test = tr("Add files with extension:"); m_pLabelTitle1->setText(&test);And it didn't work. But then I deleted the ampersand (&) and it worked! I cannot see what is the error/conflict factor here.
I'm using Qt 5.8 with MSVC v15.
@Arantxa
If you got an "Access violation" you sre supposed to run under a debugger and look at the stack trace when it happens, then you would know where the problem is.Then I tried to pass a constant QString by reference as:
const QString test = tr("Add files with extension:");
m_pLabelTitle1->setText(&test);This is not passing something by reference! It is passing it by pointer/address, which is quite different. And
QLabel::setText()does not accept aQString *so it won't compile. Standard C++.First verify that
m_pLabelTitle1->setText("Hello")does work. Then so far as I can see there is no reason whysetText(tr("Add files with extension:"));would not work ifconst QString test = tr("Add files with extension:"); m_pLabelTitle1->setText(test);does work. As I say, check in debugger.