Can someone give me a simple qt example? - qt

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):

Related

Making a layout in Qt

I just begin to work on making a layout with Grid. I tried to make it using the HBoxlayout and VBoxlayout. But How to set the position of the layout. I searched out and I found the setAlignment option but it isn't working on it.
So how to do its postioning of layouts like the image?
this is the layout I want to make
check this out
#include "mainscreen.h"
#include "ui_mainscreen.h"
#include<QLayout>
#include<QPushButton>
MainScreen::MainScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainScreen)
{
ui->setupUi(this);
QGridLayout *layout=new QGridLayout;
QHBoxLayout *hlayout=new QHBoxLayout;
QPushButton *x=new QPushButton;
hlayout->setAlignment(Qt::AlignTop);
hlayout->addWidget(x);
layout->addChildLayout(hlayout);
this->setLayout(layout);
this->show();
}
MainScreen::~MainScreen()
{
delete ui;
}
You can find good information about Layout in this Tutorial from Qt: http://doc.qt.io/qt-5/qtwidgets-layouts-basiclayouts-example.html
Two things that are important for your Layout:
You need to nest several layout. Themain layout will be a VBoxlayout that contains the top and bottom Widget and one HBoxLayout in the middle. The HBoxLayout contain the other two widgets.
Two get different size for the two middle widgtes you need to give either a Stretch factor to the Layout or define sizes for the widgets.
You may consider doing the following
QHBoxLayout *TopLayout = new QHBoxLayout;
QHBoxLayout *BottomLayout = new QHBoxLayout;
QHBoxLayout *MiddleLayout = new QHBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout;
QPushButton *topBtn = new QPushButton;
QPushButton *bottomBtn = new QPushButton;
QPushButton *LeftMiddleBtn = new QPushButton;
QPushButton *RightMiddleBtn = new QPushButton;
TopLayout->addWidget(topBtn);
BottomLayout->addWidget(bottomBtn);
// order matters here (i.e. the first addWidget will be placed in the left)
MiddleLayout->addWidget(LeftMiddleBtn);
MiddleLayout->addWidget(RightMiddleBtn);
// order matters here
mainLayout->addLayout(TopLayout);
mainLayout->addLayout(MiddleLayout);
mainLayout->addLayout(BottomLayout);
setLayout(mainLayout);
More convenient and right approach is to use Qt Designer or Qt Creator for this matter.
QMainWindow is a spatial case. It already has a layout set and it is impossible to change layout in widget! Reason is that QMainWindow has lots of other functionalities like menu, docking, status bar.
So how to make it work?
You have to set central widget:
MainScreen::MainScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainScreen)
{
ui->setupUi(this);
auto widget = new QWidget();
setCentalWidget(widget);
auto vLayout = new QVBoxLayout(widget);
// add your stuff here
// some example for testing:
vLayout->addWidget(new QButton("Test button"));
vLayout->addWidget(new QLabel("Nice label"));
vLayout->addWidget(new QTextEdit);
}
Also you are doing something wrong since this line ui->setupUi(this); indicates that you are using Qt Designer (design ui by mouse) and this means that layout should not be setup directly by code!

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!

Qt - QStackedLayout setCurrentIndex displays blank window instead of widget

I just started learning Qt a couple days ago to make a game, and I'm trying to figure out how to make layouts work.
What I want is a Window using QStackedLayout with 2 widgets inside: StartScreen and GameScreen. StartScreen is shown on top on program run. A button in StartScren is connected to a function inside Window, and Window will call setCurrentIndex to change the widget on top to GameScreen.
What happens right now is that when I click on the button, the view changes to a blank window. I've tried hardcoding to setCurrentIndex(0), which does nothing, setCurrentIndex(1), which is what GameScreen should be and displays the same blank window, and setCurrentIndex(2), which is out of index bounds but still does nothing. So the connection is going through, but I don't understand why a blank window will show up instead of the button I have on GameScreen.
If someone can explain to me what concept I missed and how to fix it, I'd greatly appreciate it. Thanks!
Here is window.cpp:
Window::Window(QWidget *parent) : QWidget(parent)
{
resize(640, 480);
layout = new QStackedLayout;
createStartScreen();
createGameScreen();
setLayout(layout);
show();
};
void Window::createStartScreen(){
start = new StartScreen();
layout->addWidget(start);
start->setWindow(this);
}
void Window::playGame(){
layout->setCurrentIndex(layout->indexOf(game));
}
void Window::createGameScreen(){
game = new GameScreen();
layout->addWidget(game);
}
startscreen.cpp:
StartScreen::StartScreen(QWidget *parent) : QWidget(parent)
{
newGameButton = new QPushButton("New Game", this);
newGameButton->setGeometry(QRect(QPoint(260, 300), QSize(120,40)));
quitButton = new QPushButton("Quit", this);
quitButton->setGeometry(QRect(QPoint(260, 360), QSize(120,40)));
connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
};
void StartScreen::setWindow(Window *w){
connect(newGameButton, SIGNAL(clicked()), w, SLOT(playGame()));
}
gamescreen.cpp:
GameScreen::GameScreen(QWidget *parent) : QWidget(parent)
{
button = new QPushButton("Hi");
button->setGeometry(QRect(QPoint(260, 260), QSize(120,40)));
};
That's because you don't call show on button. But you should rather use a layout to handle it.
e.g.:
GameScreen::GameScreen(QWidget *parent)
: QWidget(parent)
{
button = new QPushButton(tr("Hi"));
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(button);
};
button will have a size adapted to its text and will be cleaned positioned in GameScreen.
On a side note, you should rather add a signal to StartScreen to request a new game and do the connection in Window. That way you won't have a tight coupling between StartScreen and Window.

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);
}

QT4 using QMdiArea and QScrollArea strange usage trouble

Here is what I am doing: mainwindow with MdiArea, and I add a scrollarea widget (which contains a image label) to MdiArea as a subwindow. It doesn't work (the picture doesn't show).
Here is my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QScrollArea sa;
QPixmap *image = new QPixmap("2.jpg");
QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(*image);
sa.setWidget(imageLabel);
sa.show();
ui->mdiArea->addSubWindow(&sa);
}
But when I use a QLabel as subwindow directly, i.e. replace the last line with:
ui->mdiArea->addSubWindow(imageLabel);
it works perfectly.
Anyone know why this is happening?
QScrollArea sa;
This declares a QScrollArea on the stack. It gets destroyed immediately after the constructor finishes. Allocate it with new like you do for the other widgets and it should start working.
QScollArea *sa = new QScrollArea;
...
ui->mdiArea->addSubWindow(sa);
(And change the sa. to sa->.)

Resources