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();
}
Related
I'm new to Qt programming and I want to align a QLabel with a custom QGraphicsItem i created. Here's inside my custom item constructor:
QGraphicsProxyWidget* pMyProxy = new QGraphicsProxyWidget(this);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
label->setAlignment(Qt::AlignLeft);
the last line seems to have no effect whether its AlignLeft, Center, or Right.
myItem is a custom rectangle, and I'd like the label to be centered inside the rectangle, as it is the first word of label is being centered at the rectangle instead of the center of the label.
Please help
When you add a widget through a proxy, the proxy is the child of the initial item so its position will be relative to it and by default the position is (0, 0), so you see that although you set an alignment does not change, what you must do to place it in the center through setPos():
#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView w;
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsRectItem *item = new QGraphicsRectItem;
item->setRect(QRect(0, 0, 200, 200));
item->setBrush(Qt::red);
scene->addItem(item);
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(item);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(item->boundingRect().center()-label->rect().center());
w.setScene(scene);
w.show();
return a.exec();
}
Assuming this is the item, you should do the following:
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(this);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(boundingRect().center()-label->rect().center());
If you want to access the widget regarding the item you must follow the children tree:
QGraphiscScene
└── QGraphiscItem
└── (children)
QGraphicsProxyWidget
└── (widget)
QLabel
Example:
main.cpp
#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QLabel>
#include <QTimer>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView w;
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsRectItem *item = new QGraphicsRectItem;
item->setRect(QRect(0, 0, 200, 200));
item->setBrush(Qt::red);
scene->addItem(item);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setFlag(QGraphicsItem::ItemIsMovable, true);
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(item);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(item->boundingRect().center()-label->rect().center());
w.setScene(scene);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&](){
if(!scene->selectedItems().isEmpty()){
auto *item = scene->selectedItems().first();
if(!item->childItems().isEmpty()){
auto proxy = static_cast<QGraphicsProxyWidget *>(item->childItems().first());
if(proxy){
auto label = qobject_cast<QLabel *>(proxy->widget());
if(label){
label->setText(QString("x: %1, y: %2").arg(item->pos().x()).arg(item->pos().y()));
}
}
}
}
});
timer.start(100);
w.show();
return a.exec();
}
I have a hard time adding menu Bar, menus and sub menus to Qt QMainWindow programmatically.
The following code produces an error:
QWidget::setLayout: Attempting to set QLayout "" on QMainWindow "", which already has a layout
Notes :
*.The main window come out without any menu or Layout (Empty!)
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout;
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMainWindow *w = new QMainWindow;
w->setLayout(L);
w->show();
return a.exec();
}
Each QMainWindow should have a central widget:
QMainWindow *w = new QMainWindow;
QWidget* centralWidget = new QWidget;
w->setCentralWidget( centralWidget );
centralWidget->setLayout(L);
w->show();
add layout to central widget:
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include<QMenuBar>
#include<QStatusBar>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *w = new QMainWindow;
QMenuBar *menu = new QMenuBar;
QMenu *file = new QMenu();
file->addMenu("&File");
menu->addMenu(file);
QWidget *centralwidget = new QWidget(w);
w->setCentralWidget(centralwidget);
QSlider *s1 = new QSlider(Qt::Horizontal, centralwidget);
QSlider *s2 = new QSlider(Qt::Vertical, centralwidget);
QSpinBox *sb = new QSpinBox;
QHBoxLayout *L = new QHBoxLayout(centralwidget);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
w->show();
return a.exec();
}
This is the final version
#include <QApplication>
#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>
#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSlider *s1 = new QSlider(Qt::Horizontal);
QSlider *s2 = new QSlider(Qt::Vertical);
QSpinBox *sb = new QSpinBox;
QMainWindow *w = new QMainWindow;
QWidget *cw = new QWidget(w);
QMenuBar *menu = new QMenuBar(cw);
QHBoxLayout *L = new QHBoxLayout(cw);
L->addWidget(s1);
L->addWidget(s2);
L->addWidget(sb);
QMenu *file = new QMenu("&File");
file->addMenu("Open");
file->addMenu("new");
QMenu *Build = new QMenu("&Build");
Build->addAction("Rebuild this file");
Build->addAction("Rebuild All");
menu->addMenu(file);
menu->addMenu(Build);
w->setCentralWidget(cw);
w->show();
QObject::connect (s1,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s1,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), sb,SLOT(setValue(int) ) );
QObject::connect (s2,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s1,SLOT(setValue(int) ) );
QObject::connect (sb,SIGNAL(valueChanged(int) ), s2,SLOT(setValue(int) ) );
return a.exec();
}
To try to clearly answer the question suggested by the title (which is not a question by itself), QMainWindow does already have an empty menu bar by default besides other things like a central widget.
To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance.
To add a submenu to a QMenuBar, use QMenuBar::addMenu.
For example :
QAction* newAct = new QAction("save");
auto fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
auto submenu = fileMenu->addMenu("Submenu");
submenu->addAction(new QAction("action1");
submenu->addAction(new QAction("action2");
For more information, look at this Qt example : https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html and also the QMenuBar reference https://doc.qt.io/qt-5/qmenubar.html#details
I realize this is an ancient post , but the answers are unnecessary complicated
basic Qt Widget application
QApplication a(argc, argv);
MainWindow w;
w.menuBar()->addAction("TEST");
w.show();
plus
addAction("TEST");
has 4 overloads to actually implement the menu option
I have more than one widget in QGraphicsScene. The first created widget is on top of scene, other widgets is under first widget. I want such functionality - if i press one of widget it will be on top of the scene. How i can make it?
Also i don't know, how to make border to the widget in QGraphicsScene like the normal widget. This is the code:
QGraphicsScene *scene ;
QGraphicsProxyWidget *pw()
{
QWidget *w = new QWidget;
w->resize(580, 280);
w->setStyleSheet("background-color: rgb(110, 149, 255)");
QPushButton *p = new QPushButton;
p->setParent(w);
p->move(30, 50);
p->setText("hello");
QPushButton *p2 = new QPushButton;
p2->setParent(w);
p2->move(110, 50);
p2->setText("world");
QGraphicsWidget *parentWidget = new QGraphicsWidget();
parentWidget->setMinimumSize(QSizeF(100, 30));
parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
parentWidget->setAutoFillBackground(true);
parentWidget->resize(580, 280);
scene->addItem(parentWidget);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(w);
proxyWidget->setParentItem(parentWidget);
return proxyWidget;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 800, 480);
scene->addItem(pw());
scene->addItem(pw());
scene->addItem(pw());
scene->setBackgroundBrush(Qt::darkGreen);
QGraphicsView view(scene);
view.show();
return a.exec();
}
I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
I have a QWidget which contains a QVBoxLayout and that layout contains a QLabel and QToolButtons. My problem is, that the QLabel takes all the space. The only solution I found is to set maximumHeight to the QLabel, but if I do that, the Qt::AlignTop doesn't work anymore.
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
window_main.showMaximized();
return a.exec();
}
Here a picture that shows how much space the QLable takes(the blue space):
So please help to minimize the space the QLable takes :)
Your problem is that the tool buttons have a fixed size, and therefore when resizing, the label is the only type that can grow: Therefore:
After adding the label, add stretch to the layout:
layout_steps->addWidget(label_steps_start);
layout_steps->addStretch();
Modified code - adds stretch at the bottom. Label size remains fixed, and buttons remain under it. I've removed the whole main window around the outside for the sake of testing.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget_steps = new QWidget;
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start);
//--- Removed.... layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3);
layout_steps->addStretch(); //<----- Added!
widget_steps->show();
return a.exec();
}
One way you could do is to set the stretch factor for that particular widget inside the QVBoxLayout. You can find the documentation for that in here.
Basically, when you add a widget you can set that, for instance:
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget window_main;
QWidget *widget_steps = new QWidget(&window_main);
widget_steps->setFixedWidth(75);
widget_steps->move(QPoint(0, 0));
widget_steps->setStyleSheet("background-color: red;");
QVBoxLayout *layout_steps = new QVBoxLayout(widget_steps);
layout_steps->setContentsMargins(0, 0, 0, 0);
layout_steps->setSpacing(0);
QLabel *label_steps_start = new QLabel("steps:");
label_steps_start->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
label_steps_start->setStyleSheet("background-color: blue;");
layout_steps->addWidget(label_steps_start, 3, Qt::AlignTop);
layout_steps->addStretch();
QToolButton *tbutton_step1 = new QToolButton();
layout_steps->addWidget(tbutton_step1, 1);
QToolButton *tbutton_step2 = new QToolButton();
layout_steps->addWidget(tbutton_step2, 1);
QToolButton *tbutton_step3 = new QToolButton();
layout_steps->addWidget(tbutton_step3, 1);
window_main.showMaximized();
return a.exec();
}