Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel->setText with access violation
Forum Updated to NodeBB v4.3 + New Features

QLabel->setText with access violation

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 307 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • ? A Former User

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 a QString * 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 why setText(tr("Add files with extension:")); would not work if const QString test = tr("Add files with extension:"); m_pLabelTitle1->setText(test); does work. As I say, check in debugger.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved