Results 1 to 3 of 3

Thread: QTableView Set Header Columns not working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Location
    Washington D.C. area
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTableView Set Header Columns not working

    Hi,
    I am trying to build a Table View off of a SQLModel. I create the tableview when I instantiate the object, and it displays fine, but to be accurate I blow away the table and recreate it from Romote server data (a socket). After I insert the new rows/columns, I attempt to put the headers back to the way I want them in the model/View, but the Headers are right but the sizes are wrong (seems to get default sizes). Not sure what I am doing wrong here. Here some of the code I have:

    This builds the original View
    casparcg_Videos_Model = new QSqlTableModel( this,my_Db);
    casparcg_Videos_Model->setTable("CASPARCG_ENTRIES");
    casparcg_Videos_Model->select();
    casparcg_Videos_Model->setHeaderData((CG_CLS_FILENAME+1),Qt::Horizontal, tr("Video File Name"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_TYPE+1),Qt::Horizontal,tr(" Type"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_SIZE+1),Qt::Horizontal,tr(" Size"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_CREATE_DATE+1),Qt::Horizont al,tr("Creation Date"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_DURATION+1),Qt::Horizontal, tr("Duration"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_FRAMERATE+1),Qt::Horizontal ,tr("Frame Rate"));

    currentCasparWindow = new QTableView();
    currentCasparWindow->setModel(casparcg_Videos_Model);
    currentCasparWindow->setSelectionBehavior(QAbstractItemView::SelectRow s);
    currentCasparWindow->setSelectionMode(QAbstractItemView::SingleSelecti on);
    currentCasparWindow->setColumnWidth((CG_CLS_FILENAME+1),205);
    currentCasparWindow->setColumnWidth((CG_CLS_TYPE+1),45);
    currentCasparWindow->setColumnWidth((CG_CLS_SIZE+1),45);
    currentCasparWindow->setColumnWidth((CG_CLS_CREATE_DATE+1),100);
    currentCasparWindow->setColumnWidth((CG_CLS_DURATION+1),100);
    currentCasparWindow->setColumnWidth((CG_CLS_FRAMERATE+1),100);
    currentCasparWindow->resizeColumnsToContents(); // Attempt to change column sizes
    currentCasparWindow->hideColumn(CV_VIDEO_ID_COL); // Dont Show Video ID
    QHeaderView *CGheader = currentCasparWindow->horizontalHeader();
    CGheader->setStretchLastSection(true);
    currentCasparWindow->show();

    This recreates it:
    while(tcpSocket.canReadLine())
    {
    raw_Data = tcpSocket.readLine();
    hold_String = raw_Data;
    video_List= QStringList();
    if (hold_String.length() > 3) //Ignore blank lines at end...
    {
    count++;
    parse_CLS_Return(hold_String,video_List); //builds the column values...
    sql = " INSERT INTO CASPARCG_ENTRIES ";
    sql += "SET ID = " + QString::number(count) + ", ";
    sql += " FILENAME = \"" + video_List[CG_CLS_FILENAME] + "\", ";
    sql += " TYPE = \" " + video_List[CG_CLS_TYPE] + "\", ";
    sql += " SIZE = " + video_List[CG_CLS_SIZE] + ", ";
    sql += " CREATE_DATE = \" " + video_List[CG_CLS_CREATE_DATE] + "\", ";
    sql += " DURATION = " + video_List[CG_CLS_DURATION] + ", ";
    sql += " FRAMERATE = \" " + video_List[CG_CLS_FRAMERATE] + "\"";
    query.prepare(sql);
    ret = query.exec();
    if (!ret)
    {
    error_DB_Message_Box(" Failure Insert CASPARCG_ENTRIES");
    }
    }
    }
    }
    }
    cmd_Mode="";
    casparcg_Videos_Model->select();
    casparcg_Videos_Model->setHeaderData((CG_CLS_FILENAME+1),Qt::Horizontal, tr("Video File Name"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_TYPE+1),Qt::Horizontal,tr(" Type"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_SIZE+1),Qt::Horizontal,tr(" Size"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_CREATE_DATE+1),Qt::Horizont al,tr("Creation Date"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_DURATION+1),Qt::Horizontal, tr("Duration"));
    casparcg_Videos_Model->setHeaderData((CG_CLS_FRAMERATE+1),Qt::Horizontal ,tr("Frame Rate"));
    currentCasparWindow->setModel(casparcg_Videos_Model);
    currentCasparWindow->setColumnWidth((CG_CLS_FILENAME+1),205);
    currentCasparWindow->setColumnWidth((CG_CLS_TYPE+1),45);
    currentCasparWindow->setColumnWidth((CG_CLS_SIZE+1),45);
    currentCasparWindow->setColumnWidth((CG_CLS_CREATE_DATE+1),100);
    currentCasparWindow->setColumnWidth((CG_CLS_DURATION+1),100);
    currentCasparWindow->setColumnWidth((CG_CLS_FRAMERATE+1),100);

    NO matter what I do it does not seem to give me the right ColumnWidths. If I did not do the SetHeaders, then I would get the actual DB Column headings as well....

    Any ideas would be greatly appreciated.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Talking Re: QTableView Set Header Columns not working

    Set the column widths in the view by setting the section widths in the horizontalHeader()
    QHeaderView::resizeSection() and friends.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    May 2014
    Location
    Washington D.C. area
    Posts
    7
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Set Header Columns not working

    Thanks - that helped. Was able to use the HeaderPtr-> setSectionResizeMode(QHeaderView::ResizeToContents ) to make everything in the columns visible. It was exactly what I think I need (for now). Removed all of the other SetColumnSizes code.

Similar Threads

  1. Replies: 3
    Last Post: 5th January 2011, 22:55
  2. Selectable header columns
    By eekhoorn12 in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2009, 16:33
  3. Set the same header to two columns
    By ProTonS in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2009, 08:10
  4. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17
  5. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 15:29

Tags for this Thread

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.