I am trying to make a panel that shows some data, that gets added when I press a button. I will explain it trough these images:
this would be the initial state of the app, a window with a QGraphicsView
if I click "Help" it should display a window above it that never goes out of focus
I looked into using QDockWidget, but that just creates a panel next to it, that that's not what I Want. If anyone knows how to do this, I would be very grateful, thanks.
You can set children widgets in your QGraphicsView and consider it like a regular QWidget:
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
QGraphicsView* view = new QGraphicsView(scene);
view->show();
QPushButton* button = new QPushButton("Show label");
QLabel* label = new QLabel("Foobar");
QVBoxLayout* layout = new QVBoxLayout(view);
layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
layout->addWidget(button);
layout->addWidget(label);
label->hide();
QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
return app.exec();
The label will be visible in the QGraphicsView when you click on the button.
You can also embed a widget in your scene with QGraphicsProxyWidget class:
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
scene->addItem(new QGraphicsRectItem(500, 500, 50, 50));
QGraphicsView* view = new QGraphicsView(scene);
view->show();
QWidget* w = new QWidget();
QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget();
QPushButton* button = new QPushButton("Show label");
QLabel* label = new QLabel("Foobar");
QVBoxLayout* layout = new QVBoxLayout(w);
layout->addWidget(button);
layout->addWidget(label);
layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
label->hide();
QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
proxy->setWidget(w);
scene->addItem(proxy);
return app.exec();
Related
I'm writing a qt program and trying to resize mainwindow when sub widget is hidden, but there's some difference in Linux and Windows, and I don't know the property way to adjust size in both.
Here's my minimum code:
// MainWindow
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
QPushButton *hide = new QPushButton(mainWidget);
subwidget *sub = new subwidget(mainWidget);
mainlayout->addWidget(hide);
mainlayout->addWidget(sub);
connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
setCentralWidget(mainWidget);
adjustSize();
connect(hide, &QPushButton::clicked, this, [&] {
QTimer::singleShot(0, this, [&] { adjustSize(); });
});
}
// Widget
subwidget::subwidget(QWidget *parent)
: QWidget{parent}
{
QVBoxLayout *vlayout = new QVBoxLayout(this);
QPushButton *btn1 = new QPushButton();
btn2 = new QPushButton();
vlayout->addWidget(btn1);
vlayout->addWidget(btn2);
}
void subwidget::onHide()
{
btn2->hide();
}
When I set the timer interval to 1, mainwindow will be the adjusted size when btn2 is hidden in Linux, but it doesn't work in Windows. How can I adjust the mainwindow in windows?
PS: I have tried to add a signal when subwidget is hidden and connect it to adjustSize like connect(sub, &subwidget::hidden, this, [&] { adjustSize(); });, but it also doesn't work.
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 ?
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
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);
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);