PDA

View Full Version : Give a vertical header a title?



jmalicke
11th December 2014, 21:50
Is there a way to give a vertical header a title? I have attached a screenshot. I am trying to put text in the cell with the blue question mark in it.

10803

jefftee
12th December 2014, 00:11
Is there a way to give a vertical header a title? I have attached a screenshot. I am trying to put text in the cell with the blue question mark in it.
Unless I am misunderstanding your question, you give the column a title the same way you did for the other columns "State Variable" and "Volitalization Option".

How did you set the other column headings? Can you provide more about what classes you're using? i.e. QTableView, QTableWidget, etc?

jmalicke
12th December 2014, 00:20
I am inheriting from QTableView. Here is headerData which is responsible for the columns:


if (orientation == Qt::Horizontal) {
switch (section)
{
case COL_STATE_VARIABLE:
return tr("State variable");
case COL_VOLATILIZATION_OPTION:
return tr("Volatilization option");
...
else
{
SystemData & system = systemDataCopy[section];
return system.getDescription().c_str();
}

As far as I know, the column I am trying to label is not a proper table column but is instead a header itself (a vertical header). I am basically trying to give a horizontal header to my vertical headers.

ChrisW67
12th December 2014, 08:27
You can place a widget in that corner of the view using QAbstractScrollArea::setCornerWidget(). Make that widget a QLabel and you can place whatever text or image you would like. There is no way for this to be provided by a standard item model.

wysota
12th December 2014, 08:41
You can place a widget in that corner of the view using QAbstractScrollArea::setCornerWidget().

Doesn't this place the widget between scrollbars (i.e. bottom-right corner of the widget)?

ChrisW67
12th December 2014, 10:38
Oh yes, sorry, my bad %-) There is a property to enable the top-left button but I cannot see an easy way to set text there.

See: http://www.qtcentre.org/threads/6252-QTableWidget-NW-corner-header-item?p=33027#post33027 for a QTableWidget example. You will need to verify that it still works/matches given that it pokes around private internals.

jefftee
12th December 2014, 20:17
I am able to have text show up in the spot left of the horizontal header and above the vertical header in a QTableView by doing the following.

During your gui initialization:



tableview->setCornerWidgetEnabled(true);
tableview->setCornerWidgetVisible(true);
m_btn = tableview->findchild<QAbstractButton*>(); // m_btn is QAbstractButton* member variable
if (m_btn)
{
m_btn->setText("Hdr");
m_btn->installEventFilter(this);
m_btn->setEnabled(true);
m_btn->setVisible(true);
}


The event filter you installed earlier to paint the corner button:



MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::Paint && object == m_btn)
{
QStyleOptionHeader opt;
opt.init(m_btn);
QStyle::State state = QStyle::State_None;
if (m_btn->isEnabled())
state |= QStyle::Style_Enabled;
if (m_btn->isDown())
state |= QStyle::Style_Sunken;
if (m_btn->isActiveWindow())
state |= QStyle::Style_Active;
opt.state = state;
opt.rect = m_btn->rect();
opt.text = m_btn->text();
opt.position = QStyleOptionHeader::OnlyOneSection;
QStylePainter painter(m_btn);
painter.drawControl(QStyle::CE_Header, opt);
return true;
}

return QMainWindow::eventFilter(object, event);

}


I don't know how to control the vertical header's width, so if the text you're drawing is wider than the vertical header, it will clip. Hopefully this will get you started.

Regards,

Jeff