Adding layout to QStackedWidget
-
Hi,
I want to add qvboxlayout to qstackedwidget, can you please guide me?
Thanks in advance.
-
@DivyaMuthyala You don't add a layout to stacked widget: you add widgets to stacked widget and those widgets then can have layouts then.
-
Hi Jsulm,
Here my requirement is to add dynamic widgets and buttons according to data from batabase. Here based on databse tables id iam creating buttons,its ok, but now I want to add back button to work with functionality to move previous page. How can i add, can you please guide me. I am attaching part of code :
QString namestr;
QString prod_id,vendor_id,catogery_id,sub_catogery_id;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/root/evd1_6.db");
QFileInfo checkFile("/root/evd1_6.db");
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}}
Widget::~Widget()
{
delete ui;
}void Widget::on_pushButton_2_clicked()
{
close();
}void Widget::on_pushButton_clicked()
{
QSqlQuery query1("SELECT count(*) as Category_ID FROM CATEGORY");
while (query1.next())
{catogery_id=query1.record().value("Category_ID").toString(); qDebug()<<"Catogery_id is"<<catogery_id; } QWidget *wid=new QWidget(); wid->setGeometry(0,0,320,240); QVBoxLayout *layout=new QVBoxLayout(); vendormapper = new QSignalMapper(this); QSqlQuery query("SELECT Category_Name FROM CATEGORY"); while (query.next()) { ui->stackedWidget->setCurrentIndex(1); namestr=query.record().value(0).toString(); qDebug()<<"name is"<<namestr; QPushButton *button = new QPushButton(); button->setText(namestr); connect(button, SIGNAL(clicked()), vendormapper, SLOT(map())); vendormapper->setMapping(button,namestr); QPushButton *backbutton= new QPushButton(); backbutton->setText("Back"); connect(backbutton,SIGNAL(clicked()),this,SLOT(backclicked())); layout->addWidget(button); layout->addWidget(backbutton); wid->setLayout(layout); wid->show(); } connect(vendormapper, SIGNAL(mapped(const QString &)), this, SLOT(vendorselectionpage(const QString &)));
}
void Widget::backclicked()
{
qDebug()<<FUNCTION;
ui->stackedWidget->setCurrentIndex(0);
}void Widget::vendorselectionpage(const QString str)
{
QSqlQuery query1("SELECT Vendor_ID FROM VENDOR where Vendor_Name=?");
query1.addBindValue(str);
while (query1.next())
{
vendor_id=query1.record().value("Vendor_ID").toString();
qDebug()<<"Vendor_id is"<<vendor_id;
}QWidget *wid=new QWidget(); wid->setGeometry(0,0,320,240); mapper = new QSignalMapper(this); QVBoxLayout *layout=new QVBoxLayout(); QSqlQuery query("SELECT Vendor_Name FROM VENDOR"); while (query.next()) { namestr=query.record().value(0).toString(); qDebug()<<"name is"<<namestr; //ui->stackedWidget->setCurrentIndex(2); QPushButton *button = new QPushButton(); button->setText(namestr); connect(button, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button,namestr); layout->addWidget(button); wid->setLayout(layout); wid->show(); } connect(mapper, SIGNAL(mapped(const QString &)), this, SLOT(productselectionpage(const QString &)));
}
void Widget::productselectionpage(const QString str)
{
QWidget *wid=new QWidget();
wid->setGeometry(0,0,320,240);
QVBoxLayout *layout=new QVBoxLayout();
productmapper = new QSignalMapper(this);QSqlQuery query("SELECT Vendor_ID FROM VENDOR where Vendor_Name=?"); query.addBindValue(str); if(!query.exec()) { qDebug("Vendor Name Not Found"); } else { while(query.next()) { qDebug()<<"VendorId is"<<query.value(0).toString(); vendor_id = query.value(0).toString(); } } query.clear(); QSqlQuery query_Vendor("SELECT Product_Name FROM PRODUCT where Vendor_ID=?"); query_Vendor.addBindValue(vendor_id); if(!query_Vendor.exec()) { qDebug("Product Not Found"); } else { while (query_Vendor.next()) { namestr=query_Vendor.record().value(0).toString(); qDebug()<<"name is"<<namestr; //ui->stackedWidget->setCurrentIndex(2); QPushButton *button = new QPushButton(); button->setText(namestr); connect(button, SIGNAL(clicked()), productmapper, SLOT(map())); productmapper->setMapping(button,namestr); layout->addWidget(button); wid->setLayout(layout); wid->show(); } } connect(productmapper, SIGNAL(mapped(const QString &)), this, SLOT(subCatogerySelectionPage(const QString &)));
}
void Widget::subCatogerySelectionPage(const QString str)
{
qDebug()<<"Product Selected is"<<str;
QSqlQuery query1("SELECT Prod_ID,Vendor_ID,Category_ID FROM PRODUCT where Product_Name=?");
query1.addBindValue(str);
if(!query1.exec())
{
qDebug("Vendor Name Not Found");
}
else
{
while(query1.next())
{
qDebug()<<"ProdId is"<<query1.value(0).toString();
prod_id = query1.value(0).toString();
vendor_id = query1.value(1).toString();
catogery_id = query1.value(2).toString();
}
}QWidget *wid=new QWidget(); wid->setGeometry(0,0,320,240); QVBoxLayout *layout=new QVBoxLayout(); mapper = new QSignalMapper(this); qDebug()<<"Prod_Id"<<prod_id<<"Vendor_Id"<<vendor_id<<"Catogery_Id"<<catogery_id; QSqlQuery subCatogery_query("SELECT Subcat_Name FROM SUBCATEGORY where Prod_ID=? and Vendor_ID=? and Category_ID=?"); subCatogery_query.addBindValue(prod_id); subCatogery_query.addBindValue(vendor_id); subCatogery_query.addBindValue(catogery_id); if(!subCatogery_query.exec()) { qDebug("Sub-Catogery Name not found"); } else { qDebug("Completed Query"); while(subCatogery_query.next()) { qDebug()<<"Sub-Cat name is"<<subCatogery_query.record().value(0).toString(); namestr=subCatogery_query.record().value(0).toString(); qDebug()<<"name is"<<namestr; // ui->stackedWidget->setCurrentIndex(3); QPushButton *button = new QPushButton(); button->setText(namestr); connect(button, SIGNAL(clicked()), mapper, SLOT(map())); mapper->setMapping(button,namestr); layout->addWidget(button); wid->setLayout(layout); wid->show(); } }
-
@DivyaMuthyala Just put back button and stacked widget into a vbox layout.
Why do you callwid->setLayout(layout); wid->show();
in the loop? It does not make any sense.