PDA

View Full Version : how to create folders and sub folders in qt



Soumya Somasekhar Ram
29th August 2013, 11:32
In my application ,I have to create push buttons dynamically. When I clicked a particular button,a groupbox will appear.
When we right click ,a menu will appear and we can select "new folder" option. Then there should appear a push button. When ever the user clicks the right button and select new folder, a new push button should appear in the group box and all the buttons in the groupbox shold be arranged in a row by column manner.
ALL your suggestions are appreciated.Thanks in advance.

Santosh Reddy
29th August 2013, 11:43
All that description is fine but what is your question / problem.

Soumya Somasekhar Ram
30th August 2013, 06:36
Actually ,I need the code for that. I already tried the following code.
But , it create buttons in different places, not in tha particular lay out and only 2 buttons were created. In the 1st button the size is not followed. Can u suggest any idea. Thanks in advance.



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";
}

Can u suggest the code for how to delete the dynamically created button and how to rename it, whenever we want.

ChrisW67
30th August 2013, 06:49
If you want the new QPushButton to go into an existing layout then you should insert the widget into that existing layout. At the moment you are creating a new layout each time Function1 is called and that layout is unrelated to the existing layouts.

You also need to understand that the layout drives the size and position of the button, not the other way around i.e. calling resize() or move() on a widget in a layout is usually pointless.