Results 1 to 3 of 3

Thread: QTableWidget in designer

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget in designer

    Maybe it is a very lame question, but I am struggling with SIGSEGVs and really don't know what I am doing wrong...

    I have a QTableWidget constructed via designer. I can use it in a program as ui->mytable. Everything is just fine as long as I don't want to create a new QTableWidgetItem via program.

    At first, I set number of columns to 3 and rows to 0 and set text.

    Qt Code:
    1. ui->mytable->setColumnCount(3);
    2. ui->mytable->setRowCount(0);
    3.  
    4. QStringList tblLabels;
    5. tblLabels << "blah1" << "blah2" << "blah3";
    6. ui->mytable->setHorizontalHeaderLabels(tblLabels);
    To copy to clipboard, switch view to plain text mode 

    Next, I have a button which adds one single row at a time.

    Qt Code:
    1. int row;
    2. row = ui->mytable->rowCount();
    3. ui->mytable->setRowCount(row + 1);
    To copy to clipboard, switch view to plain text mode 

    This successfully adds one row, which is editable, everything is working fine. If I try to read or modify cell, which was already written from UI, everything works. But I want to predefine cell values for whole row. I am doing it as it is written in the manual. To create new items on empty row (current row), I just do:

    Qt Code:
    1. int row;
    2. QTableWidgetItem *item1 = new QTableWidgetItem("1st value");
    3. QTableWidgetItem *item2 = new QTableWidgetItem("2nd value");
    4. QTableWidgetItem *item3 = new QTableWidgetItem("3rd value");
    5. row = ui->mytable->currentRow();
    6. ui->mytable->setItem(row,0,item1);
    7. ui->mytable->setItem(row,1,item2);
    8. ui->mytable->setItem(row,2,item3);
    To copy to clipboard, switch view to plain text mode 

    The setItem call fails with SIGSEGV... Not have a clue why. I tried to create own subclass of QTableWidget and do its own initialization, again with SIGSEGV. The whole row is created with three columns, but I cannot add new items via program.

    Thank you for any help !

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget in designer

    What is the value of 'row' after this call:
    Qt Code:
    1. row = ui->mytable->currentRow();
    To copy to clipboard, switch view to plain text mode 
    Current row returns the row of the current item, and you didn't set any item to be current.
    You probably mean to use rowCount() - 1.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget in designer

    In row variable is correct value. I figured that out, my mistake was hidden in on_mytable_cellChanged() slot, which was called every time setItem was called. So error was not in initialization, but in another code which was called along.

    At end, it was very lame, as supposed (it is every time)

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2011, 04:19
  2. Replies: 1
    Last Post: 8th September 2010, 19:31
  3. Replies: 2
    Last Post: 1st October 2009, 16:26
  4. Replies: 10
    Last Post: 9th July 2007, 22:02
  5. Replies: 1
    Last Post: 22nd January 2007, 12:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.