Adding layout under QStackedWidget in Qt - c++

How can i add different layouts under QStackedWidget. I have 3 QVBoxLayout and i want to add this QVBoxLayout under QStackedWidget. I know layout cant add inside widget. but is there is any way to do that. So that from QStackedWidget i can change different layout.
mywindow::mywindow() : QMainWindow()
{
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
layout1 = new QVBoxLayout(centralWidget);
layout2 = new QVBoxLayout();
layout3 = new QVBoxLayout();
stack1 = new QStackedWidget();
list1 = new QListWidget();
list2 = new QListWidget();
list3 = new QListWidget();
label1 = new QLabel("Main Menu");
label2 = new QLabel();
label3 = new QLabel("Hello");
label4 = new QLabel("Hi");
line1 = new QLineEdit();
list1->addItem("Item 1");
list1->addItem("Item 2");
list1->addItem("Item 100");
list2->addItem("Item 3");
list2->addItem("Item 4");
list3->addItem("Item 5");
list3->addItem("Item 6");
layout1->addWidget(label1);
layout1->addWidget(list1);
layout1->addWidget(label2);
layout2->addWidget(label3);
layout2->addWidget(list2);
layout3->addWidget(label4);
layout3->addWidget(list3);
stack1->addWidget(layout1);
stack1->addWidget(layout2);
stack1->addWidget(layout3);
this->setLayout(layout);
}

Create 3 QWidget objects, add each layout to each of the widgets and then add the widgets to the stacked widget.

Related

How to fit tool bar in QDockWidget and view from QGraphicsView in a same window?

I am trying to fit tool bar from QDockWidget and view from QGraphicsView in a same window.
But it is not getting fit properly. I am expecting view window should start from below the tool bar icons. And view should occupy the whole window.
QWidget* p = new QWidget();
QBoxLayout* toolLayout = new QBoxLayout(QBoxLayout::LeftToRight,p);
toolLayout->setContentsMargins(0, 0, 0, 0);
auto toolbar = new QToolBar;
toolbar->setIconSize(QSize(45,45));
toolLayout->addWidget(toolbar,0,Qt::AlignTop);
const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/img/c.png"));
QAction* zoomIn = new QAction(newIcon, tr("&Zoom In"), this);
const QIcon newIcon1 = QIcon::fromTheme("document-new", QIcon(":/img/c1.png"));
QAction* zoomOut = new QAction(newIcon1, tr("&Zoom Out"), this);
const QIcon newIcon2 = QIcon::fromTheme("document-new", QIcon(":/img/c2.png"));
QAction* fitIn = new QAction(newIcon2, tr("&Fit In"), this);
toolbar->addAction(zoomIn);
toolbar->addAction(zoomOut);
toolbar->addAction(fitIn);
QObject::connect(zoomIn, SIGNAL(triggered()), this, SLOT(zoomIn()));
scene = new QGraphicsScene(this);
view = new QGraphicsView(this);
view->setScene(scene);
toolLayout->addWidget(view);
p->setLayout(toolLayout);
QLabel *label = new QLabel("myView");
label->setStyleSheet(
"QLabel { padding-left: 5px;font-size: 5px;"
}");
setWidget(p);
Where am I wrong ?

SetLayout of qt c++

please, help me to solve this probleme of qt;
QWidget::setLayout: Attempting to set QLayout "" on Login_1 "Login_1", which already has a layout ?
Login_1::Login_1(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Login_1)
{
ui->setupUi(this);
//The main windows
QGridLayout* MainLayout = new QGridLayout();
//The first ligne (username, line 0)
QLabel* LbNom = new QLabel("User name");
QLineEdit* LeNom = new QLineEdit();
MainLayout->addWidget(LbNom,0,0);
MainLayout->addWidget(LeNom,0,1);
//The second line (password, line 1)
QLabel* LbPassword = new QLabel("Password");
QLineEdit* LePassword = new QLineEdit(this);
MainLayout->addWidget(LbPassword,1,0);
MainLayout->addWidget(LePassword,1,1);
//Login Button(line 2)
QPushButton* PbLogin = new QPushButton(this);
PbLogin->setText("Login");
MainLayout->addWidget(PbLogin,2,0);
//setLayout(MainLayout);
}
Login_1::~Login_1()
{
delete ui;
}
Thank you for your helping...
I create at first time a new widget:
QWidget* Mywidget = new QWidget();
After tha, I make for this widget (Mywidget) parent of my odd layout:
QGridLayout* MainLayout = new QGridLayout(Mywidget);
And it work

How to add a CSS Class to a QVBoxLayout

A QWidget picks up CSS classes as expected:
leftContainerWrapper = new QVBoxLayout();
leftContainerWrapper -> setProperty("class", "top-bar-borders");
However, QVBoxLayout never do.
QVBoxLayout leftContainerWrapper = new QVBoxLayout();
leftContainerWrapper -> setProperty("class", "top-bar-borders");
How can I go about adding the style class, top-bar-borders below, to a QHBoxlayout?
.top-bar-borders {
border-top: 2px solid #5676C9;
}
You can wrap the layout inside an additional QWidget on which you set the property. For example:
QWidget * leftContainerWrapper = new QWidget();
leftContainerWrapper->setLayout(new QVBoxLayout());
leftContainerWrapper->setProperty("class", "top-bar-borders");

Why I can't display the stacked widgets in qt?

So, I'm a newbie in Qt. My code below is based from what i understand about QStackedWidgets. My problem is that I can't display the widget in index 0, which is the loginPage. when I run the code, the window that display is the mainwindow.ui window.
centralWidget = new QWidget();
loginPage = new Login;
registerPage = new Register;
userPage = new UserAccount;
stackWidget = new QStackedWidget;
stackWidget->addWidget(loginPage);
stackWidget->addWidget(registerPage);
stackWidget->addWidget(userPage);
stackWidget->setFixedSize(500,340);
layout = new QVBoxLayout;
layout->addWidget(stackWidget);
setCurrentIndex(0);
setCentralWidget(centralWidget);
centralWidget->setLayout(layout);
stackWidget->move(0,0);
centralWidget->move(0,0);
setFixedSize(500,340);
centralWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(loginPage,SIGNAL(changeCurrentIndex(int)),this,SLOT(setCurrentIndex(int)));
connect(registerPage,SIGNAL(changeCurrentIndex(int)),this,SLOT(setCurrentIndex(int)));
connect(userPage,SIGNAL(changeCurrentIndex(int)),this,SLOT(setCurrentIndex(int)));
ui->setupUi(this);

Qt widgets overlapping in layout

I have the following code in the contructor of my main window, which extends QMainWindow class:
QHBoxLayout* mainLayout = new QHBoxLayout(this);
QPushButton* button0 = new QPushButton(this);
button0->setText("Button 0");
mainLayout->addWidget(button0);
QPushButton* button1 = new QPushButton(this);
button1->setText("Button 1");
button1->setMinimumWidth(500);
mainLayout->addWidget(button1);
QPushButton* button2 = new QPushButton(this);
button2->setText("Button 2");
mainLayout->addWidget(button2);
setMinimumSize(700,480);
this->setLayout(mainLayout);
However, when I tried to run this, the buttons seem to be overlapping on the top left corner. What should I change here?
Check the console output. Usually main windows do have layout by default, then you just need to set it on the central widget instead of the window itself:
this->centralWidget()->setLayout(mainLayout);
If central widget does not exist (for example you deleted auto-generated .ui file) then just create it manually:
QWidget* centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QHBoxLayout* mainLayout = new QHBoxLayout(centralWidget);
QPushButton* button0 = new QPushButton(this);
button0->setText("Button 0");
mainLayout->addWidget(button0);
QPushButton* button1 = new QPushButton(this);
button1->setText("Button 1");
button1->setMinimumWidth(500);
mainLayout->addWidget(button1);
QPushButton* button2 = new QPushButton(this);
button2->setText("Button 2");
mainLayout->addWidget(button2);
setMinimumSize(700,480);