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. Using QTableWidget from other function avoid segfault
Forum Update on Monday, May 27th 2025

Using QTableWidget from other function avoid segfault

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 268 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.
  • B Offline
    B Offline
    Bolsvet
    wrote on last edited by
    #1

    Hello,

    I try to create separate function in class for create a QTableWidget

    fileview_widget.h

    
    public:
    
      void initImageListWidget(QStringList labels, int columns);
    
    private:
    ...
        QTableWidget *filesTable;
    ...
    

    fileview_widget.cpp

    void FileViewWidget::initImageListWidget(QStringList labels, int columns) {
    
        grid->removeWidget(imageListWidget);
    
        QTableWidget *filesTable = new QTableWidget(0, columns);
    
        filesTable->setHorizontalHeaderLabels(labels);
        filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
        filesTable->setShowGrid(false);
        filesTable->setColumnCount(columns);
        filesTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
        filesTable->setFocusPolicy(Qt::NoFocus);
    
        grid->addWidget(filesTable,0,1,2,1);
    
    }
    

    but when i use this widget from other function for insert new row i get segfault

    
            while (cnt < 128) {
                              filesTable->insertRow(cnt);  <- there are segfault
                             cnt++;
    }
    
    

    segfault trace

    #0  0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5
    [Current thread is 1 (Thread 0x7f71d1c33840 (LWP 288252))]
    (gdb) bt
    #0  0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5
    #1  0x00007f71d5f16e01 in QTableWidget::insertRow(int) () from /usr/lib/libQt5Widgets.so.5
    #2  0x00005607e3bcece4 in FileViewWidget::openFile (this=0x7ffc0644ad50, fileInfo=...) at fileview_widget.cpp
    

    if i create QTableWidget in this function - all work fine. Can you help with this problem ?

    VRoninV 1 Reply Last reply
    0
    • B Bolsvet

      Hello,

      I try to create separate function in class for create a QTableWidget

      fileview_widget.h

      
      public:
      
        void initImageListWidget(QStringList labels, int columns);
      
      private:
      ...
          QTableWidget *filesTable;
      ...
      

      fileview_widget.cpp

      void FileViewWidget::initImageListWidget(QStringList labels, int columns) {
      
          grid->removeWidget(imageListWidget);
      
          QTableWidget *filesTable = new QTableWidget(0, columns);
      
          filesTable->setHorizontalHeaderLabels(labels);
          filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
          filesTable->setShowGrid(false);
          filesTable->setColumnCount(columns);
          filesTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
          filesTable->setFocusPolicy(Qt::NoFocus);
      
          grid->addWidget(filesTable,0,1,2,1);
      
      }
      

      but when i use this widget from other function for insert new row i get segfault

      
              while (cnt < 128) {
                                filesTable->insertRow(cnt);  <- there are segfault
                               cnt++;
      }
      
      

      segfault trace

      #0  0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5
      [Current thread is 1 (Thread 0x7f71d1c33840 (LWP 288252))]
      (gdb) bt
      #0  0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5
      #1  0x00007f71d5f16e01 in QTableWidget::insertRow(int) () from /usr/lib/libQt5Widgets.so.5
      #2  0x00005607e3bcece4 in FileViewWidget::openFile (this=0x7ffc0644ad50, fileInfo=...) at fileview_widget.cpp
      

      if i create QTableWidget in this function - all work fine. Can you help with this problem ?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @Bolsvet said in Using QTableWidget from other function avoid segfault:

      QTableWidget *filesTable = new QTableWidget(0, columns);

      This is shadowing, strange your compiler doesn't give you a warning. it should be filesTable = new QTableWidget(0, columns);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JoeCFDJ 1 Reply Last reply
      2
      • VRoninV VRonin

        @Bolsvet said in Using QTableWidget from other function avoid segfault:

        QTableWidget *filesTable = new QTableWidget(0, columns);

        This is shadowing, strange your compiler doesn't give you a warning. it should be filesTable = new QTableWidget(0, columns);

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        @VRonin His warning level may be too low or the warning is simply ignored. Sometimes hungary notation may help a bit.
        private:
        QTableWidget * m_pFilesTable{};

        in .cpp it is a member variable and does not need to be redeclared.
        m_pFilesTable = new QTableWidget(0, columns);

        1 Reply Last reply
        1
        • B Offline
          B Offline
          Bolsvet
          wrote on last edited by
          #4

          Oh, my bad. Thank you ! Now working as expected.

          @JoeCFD said in Using QTableWidget from other function avoid segfault:

          @VRonin His warning level may be too low or the warning is simply ignored. Sometimes hungary notation may help a bit.

          Ok, thank you !

          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