0
$\begingroup$

Rosanswers logo

I am creating a RViz plug-in / Qt panel like in this tutorial.

There is a QTableWidget in this panel and I would like to be able to save/restore the geometry when the user closes/launches again the panel/application.

My save callback looks like this:

void DisplayTopics::save(rviz::Config config) const
                         {
  rviz::Panel::save(config);

  QByteArray table_header_state(table_->horizontalHeader()->saveState());
  // FIXME Saves an empty string, bug in RViz?
  config.mapSetValue("table_header_state", table_header_state);
}

The load callback looks like:

void DisplayTopics::load(const rviz::Config& config)
{
  rviz::Panel::load(config);

  QVariant table_header_state_var;
  if (config.mapGetValue("table_header_state", &table_header_state_var))
  {
    if (table_header_state_var.canConvert(QMetaType::QByteArray))
    {
      QByteArray table_header_state(table_header_state_var.toByteArray());

      if (!table_header_state.isEmpty())
        table_->horizontalHeader()->restoreState(table_header_state);
    }
  }
}

When the config file is saved, the table_header_state field is empty:

  - Class: topics_rviz_plugin/DisplayTopics
    Name: DisplayTopics
    table_header_state: ""

I have tested with qDebug and the QByteArray is not empty when saving.

Why is the field empty? Full code example is available here: https://gitlab.com/InstitutMaupertuis/topics_rviz_plugin/blob/table_widget_geometry/src/display_topics.cpp#L95


Originally posted by VictorLamoine on ROS Answers with karma: 1505 on 2018-03-19

Post score: 0


Original comments

Comment by gvdhoorn on 2018-03-19:
Not an answer, but I seem to remember RViz does something similar. Perhaps VisualizationFrame::saveWindowGeometry(..) can provide some info / inspiration.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The example @gvdhoorn points helped me; the state must be saved as hexadecimal and then everything works as expected once you do the right conversions, the solution is:

Save:

void DisplayTopics::save(rviz::Config config) const
                         {
  rviz::Panel::save(config);

  QByteArray table_header_state(table_->horizontalHeader()->saveState());
  // Must be saved as hex
  config.mapSetValue("table_header_state", table_header_state.toHex());
}

Load:

void DisplayTopics::load(const rviz::Config& config)
{
  rviz::Panel::load(config);

  QString table_header_state_str;
  if (config.mapGetString("table_header_state", &table_header_state_str))
  {
    table_->horizontalHeader()->restoreState(QByteArray::fromHex(qPrintable(table_header_state_str)));
  }
}

Originally posted by VictorLamoine with karma: 1505 on 2018-03-19

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by VictorLamoine on 2018-03-19:
Full example is here: https://gitlab.com/InstitutMaupertuis/topics_rviz_plugin/merge_requests/4/diffs

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.