1

I load all text files from my folder to the MainMenu in my Qt application.

void MainWindow::loadFilesToMainMenu() {
  QString pathToDir("/myfiles");

  QDirIterator it(pathToDir, QStringList() << "*.txt", QDir::Files, QDirIterator::Subdirectories);
  while (it.hasNext()) {
    QString curPathName = it.next();

    QStringList fileSegments = curPathName.split('/');
    QString curFileName = fileSegments.at(fileSegments.size() - 1);

    QAction* action = new QAction(tr(curFileName.toStdString().c_str()), this);

    action->setStatusTip(tr(curPathName.toStdString().c_str()));
    ui->menuFileList->addAction(action);

    // if new style selected?
    connect(action, SIGNAL(triggered()), this, SLOT(onLoadFile()));
  }
}

There I create QActions for all files in my folder 'myfiles' and I connect these each of these ations to the SLOT onLoadfile():

void MainWindow::onLoadFile() {
  QAction *action = qobject_cast<QAction *>(sender());
  if (action)
  {
    qDebug() << " onLoadFile " << action->data().toString();
  }
}

So each time I select one of those files in my MainMenu, this SLOt is triggered, but my debug message says:

onLoadFile ""

When I for instance select /myfiles/file1.txt

onLoadFile "/myfiles/file1.txt"

Wham am I missing? Thanx in advance

MarkZltr
  • 67
  • 2
  • 7

1 Answers1

0

The answer from @m.s. solves my question very well...

You should use QAction::setData() before trying to read the data – m.s

MarkZltr
  • 67
  • 2
  • 7