Change QWizard buttons size - qt

I am trying to do something that looks easy, but I cannot make it working. I want to make buttons bigger in my QWizard. Here is the code :
#include "wizard.h"
#include "ui_wizard.h"
#include "QAbstractButton"
Wizard::Wizard(QWidget *parent) :
QWizard(parent),
ui(new Ui::Wizard)
{
ui->setupUi(this);
QRect rect = this->button(QWizard::NextButton)->geometry();
this->button(QWizard::NextButton)->setGeometry(rect.x(), rect.y(), rect.width(), 40);
rect = this->button(QWizard::CancelButton)->geometry();
this->button(QWizard::CancelButton)->setGeometry(rect.x(), rect.y(), rect.width(), 40);
rect = this->button(QWizard::BackButton)->geometry();
this->button(QWizard::BackButton)->setGeometry(rect.x(), rect.y(), rect.width(), 40);
}
Wizard::~Wizard()
{
delete ui;
}
This code does nothing. Is it possible to change the geometry of the buttons? Or it is forbidden?
Thank you

Better is to customize user interface using QSS (Qt Style Sheet). You can read your qss file and setup stylesheet for the whole application using QApplication::setStyleSheet().
Also you can setup qss programmatically (not the best practics).
setStyleSheet("QAbstractButton { height: 50px }");
What sets height for all buttons on the widget.
In the worst case you can try this:
button(QWizard::CancelButton)->setStyleSheet("height: 50px");

Related

Can someone give me a simple qt example?

I want to implement a simple QT example: click the QPushButton to display a paragraph of text.
Like this:
I know there are many ways to implement it, but I don't know what's wrong with my code.
QPushButton *btn = new QPushButton;
//btn->show();
btn->setParent(this);
btn->setText("button 1");
QLabel *la = new QLabel(this);
connect(btn,&QPushButton::clicked,la,&QLabel::setText("show me"));
Anyone who can help me?
The idea is to create the QLabel with a predefined text and hide it. On clicking the button, show it. So, QLabel::hide and QLabel::show could be used here. Just take care of the coordinates where you show the label because it would overlap the button itself without layout or proper coordinates.
Example (mainwindow.cpp, constructor):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("My App"); // title bar text
QPushButton *btn = new QPushButton(this);
btn->setText("Click Me!");
QLabel *la = new QLabel(this);
la->setText("Show Me!"); // set label text in advance
la->move(100, 100); // set label position
la->hide(); // hide label on load
connect(btn, &QPushButton::clicked, la, &QLabel::show);
}
MainWindow::~MainWindow()
{
delete ui;
}
Output (after clicking the image):

Button is not showing on main window even after successful execution of code in Qt

I have tried this code but the button isn't displaying on the main window.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QGridLayout>
#include<QLabel>
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout();
q->addWidget(l);
this->setLayout(q);
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
I have tried to change the code even by passing enums for alignment but nothing worked.
When you create new Qt widgets Application, the default form (MainWindow ui) is created with centralWidget to put all other widgets. In your code you created the QGridLayout without a parent, typically such layout should be placed in ui->centralWidget (as far as you are not creating another widget to be set as centralWidget), moreover I assume your mainWindow is shown from main.cpp (need not use show()). your code could thus be:
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout(ui->centralWidget);
q-> addWidget(l);
Try adding the widget to the GridLayout with index using addWidget function
void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = ...)
like:
q-> addWidget(l, 0, 0);
P.S. also consider using better names for your variables!

How to set QMDIArea background as transparent in QT?

I have QMDIArea and inside it I have added a QMDISubWindow. I need QMDIArea to be completely transparent. By default it is displayed in grey color.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMdiArea area = new QMdiArea();
area->setStyleSheet("background:transparent;");
area->setAttribute(Qt::WA_TranslucentBackground);
this->setCentralWidget(area);
MyChildScreenDialog *dlg = new MyChildScreenDialog ();
area->addSubWindow(dlg );
}
But unfortunatlly, setting background:transaprent and setting attribute Qt::WA_TranslucentBackground won't work in QMdiArea.
Can we make QMdiArea as transparent? What modification we need to do in above code?
I could able to resolve this by below code.
area->setBackground(QBrush(Qt::transparent));
Using only QSS, this is the way to go:
QMdiArea {
qproperty-background: transparent;
}

How to place an icon onto a QLineEdit?

There is a Search field with the magnification-lens and a greyed out "search" keyword at the top right corner of stackoverflow.com web site:
I wonder if it is possible to achieve a same appearance with QLineEdit. If so then how?
QLineEdit* _lineEdit = new QLineEdit();
_lineEdit->setClearButtonEnabled(true);
_lineEdit->addAction(":/resources/search.ico", QLineEdit::LeadingPosition);
_lineEdit->setPlaceHolderText("Search...");
extracted from: http://saurabhg.com/programming/search-box-using-qlineedit/
Simple Way for Dummies
Add a QLineEdit, and set it frameless by QLineEdit::setFrame
Add a QLabel with background color in white (by stylesheet) and a icon
Combine the line edit and the label with a layout, set spacing to 0
Set placeholder text with QLineEdit::setPlaceholderText
Result
Advanced Way
Check this thread: "Can QLineEdit do this?"
And the related python code: http://bazaar.launchpad.net/~henning-schroeder/%2Bjunk/qtwidgets/annotate/head:/qtwidgets/lineedit.py
Or
"How to do - inside in QLineEdit insert the button.[pyqt4]"
Basically customized a QLineEdit by painting a widget(label, button or even combobox) onto it. Then reset the margin, cursor, padding and the paint event. No magics!
Here is an alternate simple way:
Set the placeholderText to "🔍" and the font Family to Seqoe UI Symbol or other font that can be found on your target systems that include the U+1F50D LEFT-POINTING MAGNIFYING GLASS glyph.
Here's a way to achieve this with stylesheets only:
QLineEdit {
background: #f3f3f3;
background-image: url(:Images/search.svg); /* actual size, e.g. 16x16 */
background-repeat: no-repeat;
background-position: left;
color: #252424;
font-family: SegoeUI;
font-size: 12px;
padding: 2 2 2 20; /* left padding (last number) must be more than the icon's width */
}
Here's the result:
It's still not perfect. You don't have much influence over the icon's position.
To have a result like this:
You can subclass the QLineEdit.
So your header should look something like this:
#ifndef LINEEDITICON_H
#define LINEEDITICON_H
#include <QLineEdit>
#include <QIcon>
class LineEditIcon : public QLineEdit
{
Q_OBJECT
public:
LineEditIcon(const QIcon icon, QWidget *parent = Q_NULLPTR);
~LineEditIcon();
void setIcon(QIcon icon);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
QIcon m_icon;
};
#endif // LINEEDITICON_H
And your source file look like:
#include "lineediticon.h"
#include <QPainter>
LineEditIcon::LineEditIcon(const QIcon icon, QWidget *parent)
: QLineEdit(parent)
{
setIcon(icon);
}
LineEditIcon::~LineEditIcon()
{
}
void LineEditIcon::setIcon(QIcon icon)
{
m_icon = icon;
if (m_icon.isNull())
setTextMargins(1, 1, 1, 1);
else
setTextMargins(20, 1, 1, 1);
}
void LineEditIcon::paintEvent(QPaintEvent * event)
{
QLineEdit::paintEvent(event);
if (!m_icon.isNull()) {
QPainter painter(this);
QPixmap pxm = m_icon.pixmap(height() - 6, height() - 6);
int x = 2, cx = pxm.width();
painter.drawPixmap(x, 3, pxm);
painter.setPen(QColor("lightgrey"));
painter.drawLine(cx + 2, 3, cx + 2, height() - 4);
}
}
Edit: A possible solution is to use a custom plugin to use it directly in QtDesigner.
QT5 addAction
```
const QIcon passwordIcon(":/new/icons/res/passwd.png");
ui->password->setClearButtonEnabled(true);
ui->password->addAction(passwordIcon, QLineEdit::LeadingPosition);
```
From QT 5.2 onwards you can do something like this. You should use pointers if you want to make it more flexible:
QIcon *ico;
ico = new QIcon(":/images/search.ico");
searchEdit->addAction(*ico, QLineEdit::LeadingPosition);

How to create resizable cusom widget on the QGraphicsScene

I study QGraphics framework and want to create custom resizable widget.
For example I created a proxy widget with QTextEdit
QGraphicsLinearLayout* l = new QGraphicsLinearLayout;
QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget;
proxy->setWidget( new QTextEdit );
proxy->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
l->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
l->addItem( proxy );
QGraphicsWidget* w = new QGraphicsWidget;
w->setLayout( l );
w->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
w->setFlag( QGraphicsItem::ItemIsMovable );
scene->addItem( w );
Widget looks fine, but I can't find out how add ability to resize it.
I searched in the Qt Examples, and google, but can't find any example.
A GraphicsItem's size, of which QGraphicsProxyWidget derives, is defined by its bounding rectangle. I expect the size of the widget would define the initial size of its proxy widget, so you could try changing the actual widget first.
In order to change the QGraphicsItem's size, you'd need to derive from QGraphicsProxyWidget and override its boundingRect() function.
Then you'd be able to create a resize function to change the returned rectangle, but ensure you call prepareGeometryChange first.
If you do inherit from QGraphicsProxyWidget and change its size this way, the enclosed widget may or may not be resized, depending upon its implementation.
I suggest you start by trying to resize the enclosing widget first.
Also note that there exists a setScale function for QGraphicsItems, which may also be an option here, as well as being able to scale the QPainter, in the paint function, if you derive from QGraphicsProxyWidget.
You should have your QGraphicsView instance resizable, so your scene content will react on resizing too.
Here it's working example (you should add QGraphicsView element on your MainWindows form):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QPushButton>
#include <QGraphicsLinearLayout>
#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsWidget *textEdit = scene->addWidget(new QTextEdit);
QGraphicsWidget *pushButton = scene->addWidget(new QPushButton);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
layout->addItem(textEdit);
layout->addItem(pushButton);
QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(layout);
scene->addItem(form);
ui->graphicsView->setScene(scene);
setCentralWidget(ui->graphicsView);
}
MainWindow::~MainWindow()
{
delete ui;
}
However this doesn't affect sizing of the widgets inside scene. To achieve that see Merlin's answer.
Also here it's example of working solution with using of scaling obtained from here:
void MyGraphicsView::resizeEvent(QResizeEvent* event)
{
QGraphicsView::resizeEvent(event);
QSize viewportSize = this->viewport().size();
QSize imageGridSize = ...; //size of all images (bounding rect)
qreal factor = viewportSize.width() / imageGridSize.width();
if( viewportSize.height() < (imageGridSize * factor).height() )
factor *= viewSize.height() / (imageSize * factor).height();
this->resetTransform();
QTransform transform = this->transform();
transform.scale(factor, factor);
this->setTransform(transform);
}

Resources