user3734823
user3734823

Reputation: 109

Combining Two functions into one

In my application, when the "open file" menu is clicked and a file is selected, the selected item(3D object file) is added on the listwidget. And if the item is double-clicked on the list, the item is displayed on the screen next to the listwidget. However, to simplify user interface, I want the item to be added on the list and displayed on the screen at the same time without double-click. In a nutshell, I'd like to combine the two functions, void MainWindow::on_actionOpen_Model_triggered() and MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item) into one.

void MainWindow::on_actionOpen_File_triggered()
{
QFileDialog diag(this);
diag.setNameFilter(tr("STL files(*.stl)"));
diag.setFileMode(QFileDialog::ExistingFiles);
diag.setViewMode(QFileDialog::Detail);
QStringList fileNames;

fileNames = diag.getOpenFileNames(this, "Open Model Files", "", "STL Files (*.stl)");
//qDebug()<<QFileInfo(fileNames).absoluteDir();
qDebug()<<fileNames;
ui->listWidget->addItems(fileNames);}

I tried to add the itemDoubleClicked function at the end of the open file function like this.

this->on_listWidget_itemDoubleClicked(ui->listWidget->currentItem());

However, whenever I open a file, the application stops running. I guess it's because the selected file is not added yet on the listwidget and in the same function the double clicked function tries to load it. Since the double click function takes QListWidgetItem *item as its argument. Combining those two seems not an easy task to me. Does anyone have an idea? It'd be appreciated.

Upvotes: 1

Views: 798

Answers (1)

RA.
RA.

Reputation: 7777

I guess it's because the selected file is not added yet on the listwidget and in the same function the double clicked function tries to load it.

No, it's because QListWidget::currentItem() retrieves the item in the list widget with focus. Merely adding items to the list widget does not give any item focus, so QListWidget::currentItem() returns a null pointer.

You have a couple of options to fix this. You can call the on_listWidget_itemDoubleClicked() function with the last item in the list widget:

QListWidgetItem* item = ui->listWidget->item(ui->listWidget->count()-1);
this->on_listWidget_itemDoubleClicked(item);

IMHO, this is a bit messy. Why not extract a function from on_listWidget_itemDoubleClicked() that takes a file name parameter? Then, just call the function from both on_listWidget_itemDoubleClicked() and on_actionOpen_File_triggered():

void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem* item)
{
    showModel(item->text());
}

void MainWindow::on_actionOpen_File_triggered()
{
    QFileDialog diag(this);
    // Code for file dialog goes here.
    QStringList fileNames;

    fileNames = diag.getOpenFileNames(this, "Open Model Files", "",
                                      "STL Files (*.stl)");
    ui->listWidget->addItems(fileNames);
    showModel(fileNames.last());
}

void MainWindow::showModel(const QString& fileName)
{
    // Code to show model goes here.
}

Upvotes: 1

Related Questions