Jacek,
I switch my table to QTableView and I created a custom Model derived from QAbstractTableModel. Unfortunately, the display is slower than before, I must have done something wrong.
In the class I have a vector of a specfic class that contains a vector of objects (a struct with a char string and its length).
When I add new rows I put the data into the vector
bool MyGameModel
::insertRow(TabbedDataParser
* tab_data_parser,
const QModelIndex &index
) {
bool result = false;
if (0 != tab_data_parser)
{
TabbedDataParser* new_data = new TabbedDataParser ();
int position = m_data_container.size ();
position = index.column ();
*new_data = *tab_data_parser;
m_data_container.push_back (new_data);
result = true;
}
return result;
}
bool MyGameModel::insertRow(TabbedDataParser* tab_data_parser, const QModelIndex &index)
{
bool result = false;
if (0 != tab_data_parser)
{
TabbedDataParser* new_data = new TabbedDataParser ();
int position = m_data_container.size ();
position = index.column ();
*new_data = *tab_data_parser;
m_data_container.push_back (new_data);
result = true;
}
return result;
}
To copy to clipboard, switch view to plain text mode
When the data is retrieved, I use the index.row to get the data from the vector, and then use my specialized obhject, to get the column data. These seems straight forward, yet I'm not gettig any improvements.
QVariant MyGameModel
::data(const QModelIndex
& index,
int role
) const {
if (index.isValid())
{
///EVEN DOING THIS IS SLOW
data
= QString ("Well? %1").
arg (index.
column ());
return data;
if (Qt::DisplayRole == role)
{
// Get the data item
int container_size = m_data_container.size ();
if ((index.row () < container_size) && (index.column () < m_column_count))
{
TabbedDataParser* cached_data = (TabbedDataParser*)m_data_container[index.row ()];
if (0 != cached_data)
{
char* value_char = 0;
int column = index.column ();
GameUpdateIndices data_index = UPDATE_INDEX_UNKNOWN;
switch (column)
{
case GAME_ID_COLUMN:
data_index = ID;
break;
case GAME_NAME_COLUMN:
data_index = NAME;
break;
case GAME_WAITING_COLUMN:
data_index = PLAYERS_WAITING;
break;
default:
break;
}
if ((UPDATE_INDEX_UNKNOWN != data_index) && (cached_data->getDataValue (data_index, value_char)))
{
data = value_char;
}
}
}
}
}
return data;
}
QVariant MyGameModel::data(const QModelIndex& index, int role) const
{
QVariant data;
if (index.isValid())
{
///EVEN DOING THIS IS SLOW
data = QString ("Well? %1").arg (index.column ());
return data;
if (Qt::DisplayRole == role)
{
// Get the data item
int container_size = m_data_container.size ();
if ((index.row () < container_size) && (index.column () < m_column_count))
{
TabbedDataParser* cached_data = (TabbedDataParser*)m_data_container[index.row ()];
if (0 != cached_data)
{
char* value_char = 0;
int column = index.column ();
GameUpdateIndices data_index = UPDATE_INDEX_UNKNOWN;
switch (column)
{
case GAME_ID_COLUMN:
data_index = ID;
break;
case GAME_NAME_COLUMN:
data_index = NAME;
break;
case GAME_WAITING_COLUMN:
data_index = PLAYERS_WAITING;
break;
default:
break;
}
if ((UPDATE_INDEX_UNKNOWN != data_index) && (cached_data->getDataValue (data_index, value_char)))
{
data = value_char;
}
}
}
}
}
return data;
}
To copy to clipboard, switch view to plain text mode
I also overrode the methods for the column header data which is a QStringList of data.
QVariant MyGameModel
::headerData (int section, Qt
::Orientation orientation,
int role
) const {
if (section < m_header_strings.count ())
{
result = m_header_strings [section];
}
return result;
}
bool MyGameModel::setHeaderData (int section, Qt::Orientation orientation, const QVariant& value, int role)
{
m_header_strings = (m_header_strings << value.toString ());
return true;
}
QVariant MyGameModel::headerData (int section, Qt::Orientation orientation, int role) const
{
QVariant result;
if (section < m_header_strings.count ())
{
result = m_header_strings [section];
}
return result;
}
bool MyGameModel::setHeaderData (int section, Qt::Orientation orientation, const QVariant& value, int role)
{
m_header_strings = (m_header_strings << value.toString ());
return true;
}
To copy to clipboard, switch view to plain text mode
I attach the model to TableView as such:
ui.m_Table->setModel (m_theModel);
ui.m_Table->setModel (m_theModel);
To copy to clipboard, switch view to plain text mode
And data is added like this:
m_theModel->insertRow ((TabbedDataParser*)&parsed_data);
m_theModel->insertRow ((TabbedDataParser*)&parsed_data);
To copy to clipboard, switch view to plain text mode
Do you see anything I missed?
Bookmarks