With the following code I generate a window with buttons:
#include <QApplication>
#include <QtWidgets>
class Item : public QWidget {
QHBoxLayout hLayout{this};
QPushButton bt{"button"};
public:
Item() : QWidget() {
hLayout.addWidget(&bt);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
vLayout.addWidget(new Item());
w.show();
return app.exec();
}
With layout.setContentsMargins(0,0,0,0) I can go to:
Is it possible to reduce even more space between buttons?
You can try using setSpacing method :
vLayout.setSpacing(0);
But I think the real issue here would be that your layout is stretched out to cover whole widget and it arranges layout items accordingly.
Another thing you can try is to set the margin :
vLayout.setMargin(0);
You can remove the space, by adjusting the spacing:
http://doc.qt.io/qt-5/qlayout.html#spacing-prop
Setting it to 0 should bring the widgets together.
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget w;
QVBoxLayout vLayout(&w);
vLayout.addWidget(new Item());
...
// Removes outer margins
vLayout.setContentsMargins(0,0,0,0);
// Set space between items to '0'
vLayout.setSpacing(0);
w.show();
return app.exec();
}
In your example above you should probably also add a strech section:
http://doc.qt.io/qt-5/qboxlayout.html#addStretch
Related
Referring to this question.
After embedding an external application with the code:
#include <QApplication>
#include<QProcess>
#include<QDesktopServices>
#include<QUrl>
#include<QWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow();
w->setWindowTitle("Genome Embedded Calculator");
QWindow* window = QWindow::fromWinId(41943047); // Hardcoded Window Id for Genome Calculator
window->setFlags(Qt::FramelessWindowHint);
QWidget *widget = QWidget::createWindowContainer(window);
w->setCentralWidget(widget);
w->show();
return a.exec();
}
How to detach the QWindow after closing the application?
I want the gnome-calculator process to continue working after closing my application.
How I can change title shown on the picture to "№". Thanks.
That widget is an object of the QTableCornerButton class that inherits from QAbstractButton but it is a class that is part of the private Qt API that does not use text, so you can not use setText() of QAbstractButton, so the other option is establish a QLabel with a layout above:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableView w;
QStandardItemModel model(10, 10);
w.setModel(&model);
QAbstractButton *button = w.findChild<QAbstractButton *>();
if(button){
QVBoxLayout *lay = new QVBoxLayout(button);
lay->setContentsMargins(0, 0, 0, 0);
QLabel *label = new QLabel("№");
label->setContentsMargins(0, 0, 0, 0);
lay->addWidget(label);
}
w.show();
return a.exec();
}
I want to use layout in qt5, but In Visual Studio 2015 the layout does not work?
Here is my code:
layout.h code
#ifndef LAYOUT_H
#define LAYOUT_H
#include <QtWidgets/QMainWindow>
#include "ui_layout.h"
class layout : public QMainWindow
{
Q_OBJECT
public:
layout(QWidget *parent = 0);
~layout();
private:
Ui::layoutClass ui;
};
#endif // LAYOUT_H
main.cpp
#include "layout.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
layout w;
QHBoxLayout hb;
QPushButton b("button 0");
QPushButton b1("button 1");
hb.addWidget(&b);
hb.addWidget(&b1);
w.setLayout(&hb);
w.show();
return a.exec();
}
Here is my result:
How to fix this problem?
QMainWindow is a special widget since it has default widgets like QStatusbar, QMenuBar, etc. When working with this widget we must place the new elements in the centralWidget.
So we must assign a widget that will be our centralWidget, and then to this we add the layouts as shown below:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
layout w;
w.setCentralWidget(new QWidget);
QHBoxLayout hb;
QPushButton b("button 0");
QPushButton b1("button 1");
hb.addWidget(&b);
hb.addWidget(&b1);
w.centralWidget()->setLayout(&hb);
w.show();
return a.exec();
}
I want to create custom Layout alike here: http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-flowlayout-cpp.html
I want some methood to put checkbox on top of custom button. For now there are
setGeometry(QRect(QPoint(...
methods for either button and checkbox but whether I'm doing it for button or checkobox first still checkbox appears "under" the button and I can't see/click it.
How can I put checkbox on top of button there?
Simply make the checkbox a child of the button and call setGeometry relative to the button. Children are always drawn on top of their parents.
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&button);
checkBox.setGeometry(button.rect());
checkBox.show();
No need to put the checkbox into a layout.
I have just made this snippet to check the checkbox on the top of the button and it works for me.
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
w.show();
return a.exec();
}
and here is if you will change the order of "parenting" and want checkbox still be on the top:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();
QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();
checkBox.setParent(NULL);
checkBox.setParent(&w);
w.show();
return a.exec();
}
I am trying to add a label to the main window using Qt. Here is a piece of the code:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget Main_Window;
QPixmap Image;
Image.load("1837.jpg");
QLabel i_label;
i_label.setPixmap(Image);
i_label.show();
QPushButton Bu_Quit("Quit", &Main_Window);
QObject::connect(&Bu_Quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Main_Window.show();
return app.exec();
}
I've been having a very hard time figuring out how to properly add QLabels to QWidgets, I tried to set the Main_Window as the main widget using this method: app.setMainWidget(Main_Window) and the label was still outside the window. So how do I put labels into widgets using Qt?
hamza, this code worked fine for me:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget Main_Window;
QLabel i_label("Start", &Main_Window);
//i_label.setPixmap(QPixmap("1837.jpg"));
QPushButton Bu_Quit("Quit" , &Main_Window);
QObject::connect(&Bu_Quit , SIGNAL(clicked()), qApp , SLOT(quit()));
QVBoxLayout *vbl = new QVBoxLayout(&Main_Window);
vbl->addWidget(&i_label);
vbl->addWidget(&Bu_Quit);
Main_Window.show();
return app.exec();
}
I commented setting the image code to show you that the label was set correctly. Make sure your image is valid (otherwise you won't see the text). The trick here was that you need to use qt layouts like QVBoxLayout
Add the label to a layout widget and set the window layout to that layout.
Design note: its better to create your own MainWindow class, inheriting from QMainWindow for instance, and design it from the inside.
or even better, use QtCreator.
You can try:
QWidget window;
QImage image("yourImage.png");
QImage newImage = image.scaled(150, 150, Qt::KeepAspectRatio);
QLabel label("label", &window);
label.setGeometry(100, 100, 100, 100);
label.setPixmap(QPixmap::fromImage(newImage));
window.show();
this way you can even decide where to put the label and choose the image size.