{
if(ui->groupBox4==object)
{
if(event
->type
() == QEvent::ContextMenu) {
// QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
//Connect the actions to slots in your main window
connect(myAction1, SIGNAL(triggered()), this, SLOT(Function1()));
connect(myAction2, SIGNAL(triggered()), this, SLOT(Function2()));
connect(myAction3, SIGNAL(triggered()), this, SLOT(Function3()));
//Add the actions to the menubar (or a menu)
menu->addAction(myAction1);
menu->addAction(myAction2);
menu->addAction(myAction3);
return false;
}
}
}
void MainWindow::Function1()
{
pb->resize(191,111);
vbox->addWidget(pb);
pb->move(370,150);
pb->show();
// vbox->addStretch(i);
// ui->groupBox4->setLayout(vbox);
}
void MainWindow::Function2()
{
qDebug()<<"delete";
}
void MainWindow::Function3()
{
qDebug()<<"Rename";
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if(ui->groupBox4==object)
{
if(event->type() == QEvent::ContextMenu)
{
// QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
QMenu *menu = new QMenu(this);
QAction* myAction1=new QAction(tr("New folder"), this);
QAction* myAction2=new QAction(tr("Delete "), this);
QAction* myAction3=new QAction(tr("Rename"), this);
//Connect the actions to slots in your main window
connect(myAction1, SIGNAL(triggered()), this, SLOT(Function1()));
connect(myAction2, SIGNAL(triggered()), this, SLOT(Function2()));
connect(myAction3, SIGNAL(triggered()), this, SLOT(Function3()));
//Add the actions to the menubar (or a menu)
menu->addAction(myAction1);
menu->addAction(myAction2);
menu->addAction(myAction3);
menu->exec(QCursor::pos());
return false;
}
}
}
void MainWindow::Function1()
{
QPushButton *pb=new QPushButton("button",this);
pb->resize(191,111);
QGridLayout *vbox = new QGridLayout;
vbox->addWidget(pb);
pb->move(370,150);
pb->show();
// vbox->addStretch(i);
// ui->groupBox4->setLayout(vbox);
}
void MainWindow::Function2()
{
qDebug()<<"delete";
}
void MainWindow::Function3()
{
qDebug()<<"Rename";
}
To copy to clipboard, switch view to plain text mode
Can u suggest the code for how to delete the dynamically created button and how to rename it, whenever we want.
Bookmarks