PDA

View Full Version : QTableView Set Header Columns not working



toadybarker
6th May 2014, 18:17
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,t r("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::Horizonta l,tr("Creation Date"));
casparcg_Videos_Model->setHeaderData((CG_CLS_DURATION+1),Qt::Horizontal,t r("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::SelectRows );
currentCasparWindow->setSelectionMode(QAbstractItemView::SingleSelectio n);
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,t r("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::Horizonta l,tr("Creation Date"));
casparcg_Videos_Model->setHeaderData((CG_CLS_DURATION+1),Qt::Horizontal,t r("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.

ChrisW67
6th May 2014, 21:47
Set the column widths in the view by setting the section widths in the horizontalHeader()
QHeaderView::resizeSection() and friends.

toadybarker
7th May 2014, 13:53
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.