I am writing a Qt application where I have QListWidget filled with QListWidgetItems and I want to create a new dialog on double click on QListWidgetItem. So I created a new dialog class as in this qt tutorial. Now, I added on_listWidget_itemDoubleClicked(QListWidgetItem* item) slot where I should create an instance of dialog class. But I want my main app to still operate and continue and just have dialog window show some info. So I cant just create an instance of dialog class and show it with exec() function.
So what i came up with is to have std::unique_ptr on my dialog class as class member of my main class, and invoke dialog with show() method. Everytime on_listWidget_itemDoubleClicked(QListWidgetItem* item) is called I will create new instance of dialog class with std::make_unique which should destroy old dialog and create a new one.
Is this generally a safe approach ? Or is there other standard way of solving such situation ?
EDIT
Here is example how I was thinking it might look:
The main class:
class USB_Packet_Analyzer : public QWidget
{
Q_OBJECT
public:
USB_Packet_Analyzer(QWidget *parent = Q_NULLPTR);
private:
Ui::USB_Packet_AnalyzerClass ui;
std::unique_ptr<DataViewer> dataViewer;
private slots:
void on_listWidget_itemDoubleClicked(QListWidgetItem* item);
};
void USB_Packet_Analyzer::on_listWidget_itemDoubleClicked(QListWidgetItem* item)
{
dataViewer = std::make_unique<DataViewer>(item, this);
dataViewer->show();
}
Dialog class :
class DataViewer : public QDialog
{
Q_OBJECT
public:
DataViewer(QListWidgetItem* item, QWidget *parent = Q_NULLPTR);
~DataViewer();
private:
Ui::DataViewer ui;
};