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. Strange questions about QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Strange questions about QTableWidget

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 5.6k Views 1 Watching
  • 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.
  • Y Offline
    Y Offline
    yeaiping
    wrote on last edited by
    #1

    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

    A smile is my sharp weapon (:

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peppy
      wrote on last edited by
      #2

      Maybe bug? It's strange, that's true...we need here some Troll :)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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?

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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();

          //...
          }
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            [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.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              [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?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sigrid
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  yeaiping
                  wrote on last edited by
                  #8

                  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.

                  A smile is my sharp weapon (:

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jim_kaiser
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • Y Offline
                      Y Offline
                      yeaiping
                      wrote on last edited by
                      #10

                      [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

                      A smile is my sharp weapon (:

                      1 Reply Last reply
                      0

                      • Login

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