My program has to QListwidgets. When I right click on the list I have a QDialog popup. When the QDialog pops up I would like to know which QListWidget has opened it. I tried using this line of code
qDebug()<<QDialog->parentWidget()->objectName();
qDebug()<<QDialog->parentWidget()->objectName();
To copy to clipboard, switch view to plain text mode
but it crashed my program.
Here is some code from my .cpp file.
(this is in the main function/constructor)
lists->setFixedWidth(200);
etfList->setObjectName("ETFList");
etfList->setContextMenuPolicy(Qt::CustomContextMenu);
ipoList->setObjectName("IPOList");
ipoList->setContextMenuPolicy(Qt::CustomContextMenu);
lists->addTab(etfList, "ETF List");
lists->addTab(ipoList, "IPO List");
connect(etfList,
SIGNAL(customContextMenuRequested
(const QPoint &)),
this,
SLOT(menuPopup
(const QPoint &)));
connect(ipoList,
SIGNAL(customContextMenuRequested
(const QPoint &)),
this,
SLOT(menuPopup
(const QPoint &)));
lists = new QTabWidget;
lists->setFixedWidth(200);
etfList = new QListWidget;
ipoList = new QListWidget;
etfList->setObjectName("ETFList");
etfList->setContextMenuPolicy(Qt::CustomContextMenu);
ipoList->setObjectName("IPOList");
ipoList->setContextMenuPolicy(Qt::CustomContextMenu);
lists->addTab(etfList, "ETF List");
lists->addTab(ipoList, "IPO List");
connect(etfList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &)));
connect(ipoList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &)));
To copy to clipboard, switch view to plain text mode
(this is the slot)
void MainWindow
::menuPopup(const QPoint &point
){
qDebug()<<dia->parentWidget()->objectName(); <---crashes on this line
dia->show();
}
}
void MainWindow::menuPopup(const QPoint &point){
QDialog *dia = new QDialog();
qDebug()<<dia->parentWidget()->objectName(); <---crashes on this line
dia->show();
}
}
To copy to clipboard, switch view to plain text mode
And in my header file:
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
signals:
private slots:
void menuPopup
(const QPoint &point
);
class MainWindow : public QMainWindow
{
Q_OBJECT
QTabWidget *lists;
QListWidget *etfList;
QListWidget *ipoList;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
private slots:
void menuPopup(const QPoint &point);
To copy to clipboard, switch view to plain text mode
Bookmarks