Soumya Somasekhar Ram
10th September 2013, 08:37
When the user right clicks to a particular groupbox,menu will appear.
depends on the menu item triggered it will go to different functions for creating deleting and renaming the button.
The code is given below
QString instance;
QMap<QString, QToolButton*> unselectButtonMap;
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);
QAction* myAction4=new QAction(tr("select file type"), 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()));
connect(myAction4, SIGNAL(triggered()), this, SLOT(Function4()));
//Add the actions to the menubar (or a menu)
menu->addAction(myAction1);
menu->addAction(myAction2);
menu->addAction(myAction3);
menu->addAction(myAction4);
menu->exec(QCursor::pos());
return false;
}
}
}
void MainWindow::Function1()
{
i++;
index++;
int positionr = layout1->rowCount();
int positionc = layout1->columnCount();
QPixmap* pixmap5 = new QPixmap("../nimhans/yellow2.jpg");
QIcon icon5(*pixmap5);
QToolButton* b = new QToolButton(this);
b->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
b->setIcon(icon5);
b->setIconSize(pixmap5->rect().size()/2);
b->setText(QString(" New folder") + QString::number(i));
b-> setObjectName(QString(" New folder") + QString::number(i));
unselectButtonMap[instance] = b;
unselectButtonMap[instance]->installEventFilter(this);
qDebug()<<unselectButtonMap[instance];
qDebug()<<unselectButtonMap[instance]->text();
buttons[index]=unselectButtonMap[instance];
if(i<=5)
layout1->addWidget(b, 0, positionc);
else if(i>5 && i<=10)
layout1->addWidget(b, 1,i-5);
else if(i>10 && i<=15)
layout1->addWidget(b, 2,( i-10));
else
QMessageBox::warning(0,tr("Warning"),tr("Can not create more folders"));
}
void MainWindow::Function2()
{
foreach( instance,unselectButtonMap.keys() )
if(unselectButtonMap[instance]== QObject::sender()) {
QMessageBox::warning(0,tr("Warning"),tr("do you want to delete the folder"));
unselectButtonMap[instance]->deleteLater();
}
}
}}
But here QObject::sender() will return QAction which is triggered in the menu.
How can I get the clicked push button reference(sender object)??
Can u suggest any method??
All your suggestions are appreciable .Thanks in advance.
depends on the menu item triggered it will go to different functions for creating deleting and renaming the button.
The code is given below
QString instance;
QMap<QString, QToolButton*> unselectButtonMap;
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);
QAction* myAction4=new QAction(tr("select file type"), 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()));
connect(myAction4, SIGNAL(triggered()), this, SLOT(Function4()));
//Add the actions to the menubar (or a menu)
menu->addAction(myAction1);
menu->addAction(myAction2);
menu->addAction(myAction3);
menu->addAction(myAction4);
menu->exec(QCursor::pos());
return false;
}
}
}
void MainWindow::Function1()
{
i++;
index++;
int positionr = layout1->rowCount();
int positionc = layout1->columnCount();
QPixmap* pixmap5 = new QPixmap("../nimhans/yellow2.jpg");
QIcon icon5(*pixmap5);
QToolButton* b = new QToolButton(this);
b->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
b->setIcon(icon5);
b->setIconSize(pixmap5->rect().size()/2);
b->setText(QString(" New folder") + QString::number(i));
b-> setObjectName(QString(" New folder") + QString::number(i));
unselectButtonMap[instance] = b;
unselectButtonMap[instance]->installEventFilter(this);
qDebug()<<unselectButtonMap[instance];
qDebug()<<unselectButtonMap[instance]->text();
buttons[index]=unselectButtonMap[instance];
if(i<=5)
layout1->addWidget(b, 0, positionc);
else if(i>5 && i<=10)
layout1->addWidget(b, 1,i-5);
else if(i>10 && i<=15)
layout1->addWidget(b, 2,( i-10));
else
QMessageBox::warning(0,tr("Warning"),tr("Can not create more folders"));
}
void MainWindow::Function2()
{
foreach( instance,unselectButtonMap.keys() )
if(unselectButtonMap[instance]== QObject::sender()) {
QMessageBox::warning(0,tr("Warning"),tr("do you want to delete the folder"));
unselectButtonMap[instance]->deleteLater();
}
}
}}
But here QObject::sender() will return QAction which is triggered in the menu.
How can I get the clicked push button reference(sender object)??
Can u suggest any method??
All your suggestions are appreciable .Thanks in advance.