ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How do I save a QByteArray in my Qt panel?

asked 2018-03-19 07:44:37 -0600

VictorLamoine gravatar image

updated 2018-03-19 07:45:42 -0600

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...

edit retag flag offensive close merge delete

Comments

1

Not an answer, but I seem to remember RViz does something similar. Perhaps VisualizationFrame::saveWindowGeometry(..) can provide some info / inspiration.

gvdhoorn gravatar image gvdhoorn  ( 2018-03-19 08:32:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-03-19 09:08:00 -0600

VictorLamoine gravatar image

updated 2018-03-19 09:09:02 -0600

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)));
  }
}
edit flag offensive delete link more

Comments

VictorLamoine gravatar image VictorLamoine  ( 2018-03-19 09:22:34 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-03-19 07:44:37 -0600

Seen: 305 times

Last updated: Mar 19 '18