I am trying to create layouts in my MainWindow class dynamically. I have four frames which are laid with a grid layout object. Each frame contains a custom ClockWidget. I want the ClockWidget objects to resize accordingly when I resize the main window, so I need to add them to a layout. However, I need to do this at runtime, since the object itself is created at runtime. I tried to accomplish this programmatically, but the commented-out code below attempting to create a new layout causes the program to crash. What is the procedure for doing this correctly?
Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ClockView.h"
namespace Ui{
class MainWindow;
}
class QLayout;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void populateViewGrid();
private:
Ui::MainWindow *ui;
ClockView *clockView_1;
ClockView *clockView_2;
ClockView *clockView_3;
ClockView *clockView_4;
QLayout *layout_1;
QLayout *layout_2;
QLayout *layout_3;
QLayout *layout_4;
};
#endif // MAINWINDOW_H
implementation file:
#include <QVBoxLayout>
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
populateViewGrid();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::populateViewGrid()
{
clockView_1 = new ClockView(ui->frame_1);
clockView_2 = new ClockView(ui->frame_2);
clockView_3 = new ClockView(ui->frame_3);
clockView_4 = new ClockView(ui->frame_4);
/*
layout_1 = new QVBoxLayout;
layout_2 = new QVBoxLayout;
layout_3 = new QVBoxLayout;
layout_4 = new QVBoxLayout;
layout1->addWidget(clockView_1);
layout2->addWidget(clockView_2);
layout3->addWidget(clockView_3);
layout4->addWidget(clockView_4);
ui->frame_1->setLayout(layout_1);
ui->frame_2->setLayout(layout_2);
ui->frame_3->setLayout(layout_3);
ui->frame_3->setLayout(layout_4);
*/
}
Your procedure is correct. There are some typos, for example, you're setting the layout twice for frame3. That may be your problem. Crashes aren't always reproducible. I don't think you have any other problems than that. Below is a self contained example. It also keeps all the instances by value, avoiding the premature pessimization of an extra dereference via a pointer.
// https://github.com/KubaO/stackoverflown/tree/master/questions/dynamic-widget-10790454
#include <cmath>
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
#include <array>
// Interface
class ClockView : public QLabel
{
public:
explicit ClockView(QWidget* parent = nullptr) : QLabel(parent)
{
static int ctr = 0;
setText(QString::number(ctr++));
}
};
class MainWindow : public QMainWindow
{
public:
explicit MainWindow(QWidget *parent = nullptr);
void populateViewGrid();
private:
static constexpr int N = 10;
QWidget central{this};
QGridLayout centralLayout{¢ral};
std::array<QFrame, N> frames;
std::array<ClockView, N> clockViews;
std::array<QVBoxLayout, N> layouts;
};
// Implementation
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setCentralWidget(¢ral);
const int n = ceil(sqrt(N));
for (int i = 0; i < N; ++ i) {
frames[i].setFrameShape(QFrame::StyledPanel);
centralLayout.addWidget(&frames[i], i/n, i%n, 1, 1);
}
populateViewGrid();
}
void MainWindow::populateViewGrid()
{
for (int i = 0; i < N; ++ i) {
layouts[i].addWidget(&clockViews[i]);
frames[i].setLayout(&layouts[i]);
}
}
int main(int argc, char** argv)
{
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
And the qmake project file.
greaterThan(QT_MAJOR_VERSION, 4) {
QT = widgets
CONFIG += c++11
} else {
QT = gui
unix:QMAKE_CXXFLAGS += -std=c++11
macx {
QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
}
}
TARGET = dynamic-widget-10790454
TEMPLATE = app
SOURCES += main.cpp
Related
I have a simple Qt application that dynamically adds and deletes items from the layout, but it does not work, so I wrote the code below to illustrate what I'm doing:
The header file justs put an additional layout field in addition to Qt creator template:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFrame>
#include <QVBoxLayout>>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QFrame* create_frame();
void remove_frame();
QVBoxLayout* layout;
};
#endif // MAINWINDOW_H
The cpp file adds a few QFrame to the current layout, then clicking the button will remove them one by one. The QCoreApplication::processEvents and QThread::sleep just adds some animation to show what is happening.
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QPushButton>
#include <QThread>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto window = new QWidget(this);
setCentralWidget(window);
auto button = new QPushButton(window);
button->setGeometry(100, 100, 100, 100);
layout = new QVBoxLayout;
window->setLayout(layout);
for (int i = 0; i < 5; i++) {
auto x = create_frame();
layout->addWidget(x);
}
connect(button, &QPushButton::clicked, this, &MainWindow::remove_frame);
}
void MainWindow::remove_frame() {
while(layout->count() > 0) {
auto item = layout->itemAt(0);
layout->removeItem(item);
delete item;
QCoreApplication::processEvents();
QThread::msleep(200);
}
}
QFrame* MainWindow::create_frame() {
auto x = new QFrame();
x->setMinimumHeight(20);
x->setMaximumHeight(20);
x->setFrameStyle(QFrame::StyledPanel);
return x;
}
MainWindow::~MainWindow()
{
delete ui;
}
The animation is a mess, and does not completely remove all the QFrame boxes. What did I do wrong here? And later I find that if I use auto item = layout->itemAt(0)->widget(); layout->removeWidget(item); then I can indeed remove them, are removeItem and removeWidget doing different things?
There is a working app and I am adding a new language for GUI. Everything works fine but some part of the app are simply not translated. QLinguist detects all of them, and I have added a new translations, but still no result.
Here is the fragment of the code that is not getting translation:
"imagecropwindow_p.h":
#include <QWidget>
class QLabel;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class QFrame;
class CropWindowComponents: public QWidget
{
public:
CropWindowComponents(QWidget *parent = nullptr);
QPushButton *changeBtn;
QPushButton *cropBtn;
QPushButton *continueBtn;
QPushButton *cancelBtn;
};
class HorizontalWindow : public CropWindowComponents
{
Q_OBJECT
public:
HorizontalWindow(QWidget *parent = nullptr);
};
class VerticalWindow : public CropWindowComponents
{
Q_OBJECT
public:
VerticalWindow(QWidget *parent = nullptr);
};
"imagecropwindow_p.cpp":
#include "imagecropwindow_p.h"
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
CropWindowComponents::CropWindowComponents(QWidget *parent) :
QWidget(parent)
{
changeBtn = new QPushButton(tr("Change"), this);
cropBtn = new QPushButton(tr("Crop"), this);
continueBtn = new QPushButton(tr("Continue"), this);
cancelBtn = new QPushButton(tr("Cancel"), this);
}
HorizontalWindow::HorizontalWindow(QWidget *parent) :
CropWindowComponents(parent)
{
QHBoxLayout *btnsLyt = new QHBoxLayout;
btnsLyt->setMargin(0);
btnsLyt->addWidget(changeBtn);
btnsLyt->addWidget(cropBtn);
btnsLyt->addWidget(continueBtn);
btnsLyt->addWidget(cancelBtn);
btnsLyt->addStretch();
setLayout(btnsLyt);
}
VerticalWindow::VerticalWindow(QWidget *parent) :
CropWindowComponents(parent)
{
QVBoxLayout *btnsLyt = new QVBoxLayout;
btnsLyt->setMargin(0);
btnsLyt->addWidget(changeBtn);
btnsLyt->addWidget(cropBtn);
btnsLyt->addWidget(continueBtn);
btnsLyt->addWidget(cancelBtn);
btnsLyt->addStretch();
setLayout(btnsLyt);
}
"imagecropperwindow.h":
#include "imagecropwindow_p.h"
class ImageCropperWindow : public QWidget
{
Q_OBJECT
public:
explicit ImageCropperWindow(QWidget *parent = nullptr);
private slots:
void changeWindowOrientation();
private:
HorizontalWindow *horizWindow;
VerticalWindow *verticalWindow;
};
"imagecropperwindow.cpp":
#include "imagecropperwindow.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
ImageCropperWindow::ImageCropperWindow(QWidget *parent) : QWidget(parent)
{
horizWindow = new HorizontalWindow(this);
verticalWindow = new VerticalWindow(this);
connect(horizWindow->changeBtn, &QPushButton::clicked,
this, &ImageCropperWindow::changeWindowOrientation);
connect(verticalWindow->changeBtn, &QPushButton::clicked,
this, &ImageCropperWindow::changeWindowOrientation);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setMargin(3);
layout->addWidget(horizWindow);
layout->addWidget(verticalWindow);
verticalWindow->setVisible(false);
setLayout(layout);
}
void ImageCropperWindow::changeWindowOrientation()
{
if (horizWindow->isVisible()) {
horizWindow->setVisible(false);
verticalWindow->setVisible(true);
}
else {
verticalWindow->setVisible(false);
horizWindow->setVisible(true);
}
this->resize(this->minimumSizeHint());
}
And "main.cpp":
#include <QApplication>
#include <QDebug>
#include <QTranslator>
#include "imagecropperwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator newLang;
newLang.load(":/translations/newLanguage.qm");
a.installTranslator(&newLang);
ImageCropperWindow w;
w.show();
return a.exec();
}
You can find the whole project here.
PS: For an example case, I added stars (*) as a new translation, such as
nativeLanuage - ***newLanguage***
Change - ***Change***
Crop - ***Crop***
Continue - ***Continue***
Cancel - ***Cancel***
QTranslator uses the MOC to do the translations so if you want that if you want your widget to be translated you should use the macro Q_OBJECT, in your case CropWindowComponents does not have it, so the solution is to add it:
imagecropwindow_p.h
class CropWindowComponents: public QWidget
{
Q_OBJECT # <--- add this
public:
...
On the other hand do not add the .ts to the .qrc since the .ts only serves to convert it to the .qm binary. When you add a file to the .qrc it is compiled and added to the executable, increasing the size of the last one. Therefore adding the .ts increases the size of the executable unnecessarily.
I'm pretty new to c++ and Qt but I'm trying to create a simple battleship game. So far I have been able to set up the grid and also place ships but I wan't to put in a menu before the main game window where you choose "difficulty" which will determine how big the grid size for battleship is.
The code for drawing the game board goes as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "boardrender.h"
#include "playerrender.h"
#include "../BattleScrabble/BattleshipBoard.h"
#include "../BattleScrabble/HumanPlayer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// BattleshipBoard *a = new BattleshipBoard(10);
// Ship *b = new Ship(AircraftCarrier);
ui->battleshipBoardView->setMouseTracking(true);
// BoardRender *scene=new BoardRender(a, ui->battleshipBoardView->width(), this);
// ui->battleshipBoardView->setScene(scene);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
Player *b = new HumanPlayer(10,0);
PlayerRender *a = new PlayerRender(b, ui->battleshipBoardView,this);
}
MainWindow::~MainWindow()
{
delete ui;
}
where new HumanPlayer(10,0); is the grid size.
And now the interesting part, the difficulty window:
#include "difficultywindow.h"
#include "ui_difficultywindow.h"
#include "DifficultyWindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
DifficultyWindow::DifficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::DifficultyWindow)
{
ui->setupUi(this);
}
DifficultyWindow::~DifficultyWindow()
{
delete ui;
}
void DifficultyWindow::on_pushButton_clicked()
{
mWindow = new MainWindow(this);
mWindow->show();
}
when I'm instantiating the "new MainWindow" is there a way to change the HumanPlayer parameters in this class instead?
Any help is greatly appreciated. Thanks :)
edit: and my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "difficultywindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DifficultyWindow w;
w.show();
return a.exec();
}
You have to make some modifications, to make your code more understandable you must first add a parameter to MainWidow, in this case it will be called grid
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(int grid, QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Player *b;
PlayerRender *g;
};
mainwindow.cpp
MainWindow::MainWindow(int grid, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->battleshipBoardView->setMouseTracking(true);
ui->battleshipBoardView->resize(ui->battleshipBoardView->width(),ui->battleshipBoardView->width());
b = new HumanPlayer(grid, 0);
g = new PlayerRender(b, ui->battleshipBoardView,this);
}
Then we will create an enumeration and to handle a signal instead of 3 we will use QButtonGroup:
difficultyWindow.h
class difficultyWindow : public QDialog
{
Q_OBJECT
public:
enum Difficult{
EASY,
MEDIUM,
HARD
};
explicit difficultyWindow(QWidget *parent = 0);
~difficultyWindow();
private slots:
void onClicked(int id);
private:
Ui::difficultyWindow *ui;
MainWindow *mWindow;
QButtonGroup *group;
};
difficultyWindow.h
difficultyWindow::difficultyWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::difficultyWindow)
{
ui->setupUi(this);
group = new QButtonGroup(this);
group->addButton(ui->easyDiffButton, EASY);
group->addButton(ui->mediumDifficultyButton, MEDIUM);
group->addButton(ui->hardDifficultyButton, HARD);
connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onClicked(int)));
}
difficultyWindow::~difficultyWindow()
{
delete ui;
}
void difficultyWindow::onClicked(int id)
{
int grid;
switch (id) {
case EASY:
grid = 10;
break;
case MEDIUM:
grid = 20;
break;
case HARD:
grid = 30;
break;
}
mWindow = new MainWindow(grid, this);
mWindow->show();
}
Note: eliminate the slots, its are unnecessary for this case.
I'm working on an application where I need to be able to draw a line between two QWidget objects. I have tried quite a few things, but my current attempt (which I think is in the right direction I just think I'm missing something) is to have the containing widget (which I called DrawWidget and which holds the QGridLayout that the QWidget objects are added to) override the paintEvent method and call the QPainter::drawLine() function.
The issues I'm having are that:
No matter how I try to get the position of the widgets, the endpoints of the line are always in the wrong place
Whenever I try to draw a second line, the first line that I drew gets erased.
Here is the paintEvent function of the containing widget:
void paintEvent(QPaintEvent *)
{
if (!drewSinceUpdate){
drewSinceUpdate = true;
QPainter painter(this);
painter.setPen(QPen(Qt::black));
painter.drawLine(start->geometry().center(), end->geometry().center());
}
}
I have tried many different ways to get the correct position of the widgets in the last line of paintEvent, which I will post some of the ways (I can't remember all of them):
painter.drawLine(start->pos(), end->pos());
painter.drawLine(start->mapToGlobal(start->geometry().center()), end->mapToGlobal(end->geometry().center()));
painter.drawLine(this->mapToGlobal(start->geometry().center()), this->mapToGlobal(end->geometry().center()));
painter.drawLine(start->mapTo(this, start->pos()), end->mapTo(this, end->pos()));
painter.drawLine(this->mapFrom(start, start->pos()), this->mapFrom(end, end->pos()));
And just to make my question clear, here is an example of what I am looking for, taken from QT Diagram Scene Example:
But this is what I end up getting:
Thank you for any help you can provide.
NOTE:
-start and end are both QWidget objects which I passed in using another method
-The hierarchy relevant to DrawWidget is:
QMainWindow
->QScrollArea
->DrawWidget
->QGridLayout
->Items <-- These are the things I want to connect
EDIT: To make a Complete and Verifiable example, here is the entirety of the relevant code.
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Setting up the relevant hierarchy
ui->setupUi(this);
scrollArea = new QScrollArea();
setCentralWidget(scrollArea);
drawWidget = new DrawWidget();
gridLayout = new QGridLayout();
gridLayout->setSpacing(300);
drawWidget->setLayout(gridLayout);
scrollArea->setWidget(drawWidget);
scrollArea->setWidgetResizable(true);
AddItemSlot();
QApplication::connect(scrollArea->horizontalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(scrollHorizontal()));
}
// This is just creating a single one of the example widgets which I want to connect
QWidget* MainWindow::CreateNewItem(){
QWidget* itemWidget = new QWidget();
itemWidget->setStyleSheet("background-color: lightgray");
QHBoxLayout* singleItemLayout = new QHBoxLayout();
itemWidget->setLayout(singleItemLayout);
QTextEdit* textEdit = new QTextEdit(std::to_string(counter++).c_str());
textEdit->setStyleSheet("background-color:white;");
singleItemLayout->addWidget(textEdit);
QVBoxLayout* rightSidePanel = new QVBoxLayout();
rightSidePanel->setAlignment(Qt::AlignTop);
QPushButton* button1 = new QPushButton("Top Button");
QApplication::connect(button1, SIGNAL(clicked(bool)), this, SLOT(AddItemSlot()));
rightSidePanel->addWidget(button1);
QWidget* rightPanelWidget = new QWidget();
rightSidePanel->setMargin(0);
rightPanelWidget->setLayout(rightSidePanel);
singleItemLayout->addWidget(rightPanelWidget);
itemWidget->setLayout(singleItemLayout);
itemWidget->setMinimumWidth(400);
itemWidget->setFixedSize(400,200);
return itemWidget;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::scrollHorizontal()
{
scrollArea->ensureWidgetVisible(noteItems.back());
}
void MainWindow::AddItemSlot()
{
QWidget* w = CreateNewItem();
gridLayout->addWidget(w,currRow, currCol++);
if (!noteItems.empty()){
drawWidget->updateEndpoints(noteItems.back(), w);
}
noteItems.push_back(w);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGridLayout>
#include <QWidget>
#include <QMainWindow>
#include <QScrollArea>
#include <drawwidget.h>
#include "drawscrollarea.h"
#include <vector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void scrollHorizontal();
void AddItemSlot();
private:
Ui::MainWindow *ui;
QWidget* CreateNewItem();
int counter = 0, currCol = 0, currRow = 0;
std::vector<QWidget*> noteItems;
QScrollArea* scrollArea;
DrawWidget* drawWidget;
QGridLayout* gridLayout;
};
#endif // MAINWINDOW_H
DrawWidget.cpp:
#include "drawwidget.h"
#include <QDebug>
#include <QRect>
DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
}
void DrawWidget::paintEvent(QPaintEvent *)
{
if (!drewSinceUpdate){
drewSinceUpdate = true;
QPainter painter(this);
painter.setPen(QPen(Qt::black));
for (ConnectedPair pair : items){
const QWidget* from = pair.from;
const QWidget* to =pair.to;
QPoint start = from->mapToGlobal(from->rect().topRight() + QPoint(0, from->height()/2));
QPoint end = to->mapToGlobal(to->rect().topLeft() + QPoint(0, to->height()/2));
painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
}
}
}
void DrawWidget::updateEndpoints(QWidget* startIn, QWidget* endIn){
drewSinceUpdate = false;
items.push_back(ConnectedPair{startIn, endIn});
}
DrawWidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
#include <QPainter>
#include <QtCore>
#include <vector>
class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = nullptr);
void updateEndpoints(QWidget* startIn, QWidget* endIn);
virtual void paintEvent(QPaintEvent *);
signals:
private:
struct ConnectedPair {
const QWidget* from;
const QWidget* to;
};
std::vector<ConnectedPair> items;
bool drewSinceUpdate = true;
};
#endif // DRAWWIDGET_H
For this case we use the function mapToGlobal() and mapfromGlobal(), since pos() returns a position with respect to the parent and this can cause problems if the widget has different parents.
drawwidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = nullptr);
void addWidgets(const QWidget *from, const QWidget *to);
protected:
void paintEvent(QPaintEvent *);
private:
struct WidgetsConnected {
const QWidget* from;
const QWidget* to;
};
QList<WidgetsConnected> list;
};
#endif // DRAWWIDGET_H
drawwidget.cpp
#include "drawwidget.h"
#include <QPainter>
DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
}
void DrawWidget::addWidgets(const QWidget * from, const QWidget * to)
{
list.append(WidgetsConnected{from , to});
update();
}
void DrawWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
for(const WidgetsConnected el: list){
const QWidget* from = el.from;
const QWidget* to = el.to;
QPoint start = from->mapToGlobal(from->rect().topRight() + QPoint(0, from->height()/2));
QPoint end = to->mapToGlobal(to->rect().topLeft() + QPoint(0, to->height()/2));
painter.drawLine(mapFromGlobal(start), mapFromGlobal(end));
}
}
The complete example can be found here.
I was trying to add a QWidget while runtime in Qt but It is showing SIGSEV signal received from OS because of segmentation fault.
Here is my code:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_submit_clicked();
private:
Ui::MainWindow *ui;
QLabel *label;
QLineEdit *line_edit;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
label = new QLabel();
label->setText("Success");
MainWindow.layout->addWidget(label);
label->show();
}
else
{
line_edit = new QLineEdit();
line_edit->setText("Sorry");
MainWindow.layout->addWidget(line_edit);
line_edit->show();
}
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I know that segmentation fault occurs due to dereferencing of a null pointer but i couldn't find where I have done that mistake.Any Suggestions?
MainWindow.layout->addWidget(label);
doesn't make a lot of sense - this should not even compile, as Sebastian noted.
First, make sure you have layout in the Ui file (I added one vertical layout named verticalLayout), so you have a layout where you will add widgets. You will have a pointer to it inside your ui object.
Now, just use addWidget on that layout and everything should work:
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
QLabel *label = new QLabel();
label->setText("Success");
ui->verticalLayout->addWidget(label);
// label->show(); widgets will became the part of the MainWindow, as the addWidget
// will add them into the hierarchy.
}
else
{
QLineEdit *line_edit = new QLineEdit();
line_edit->setText("Sorry");
ui->verticalLayout->addWidget(line_edit);
// line_edit->show()
}
}
Note - addWidget will set the owner of the widget to be the layout, so the widget will be deleted on the destruction of the layout.
Maybe implementing in this way will make sense?
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
QWidget *w = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout; // creates a vertical layout
if(str=="rana"&&str1=="vivek")
{
label = new QLabel(w);
label->setText("Success");
layout->addWidget(label);
}
else
{
line_edit = new QLineEdit(w);
line_edit->setText("Sorry");
layout->addWidget(line_edit);
}
w->setLayout(layout);
setCentralWidget(w);
}
UPDATE:
QMainWindow already has a predefined layout, so it was needless to introduce a new one. The code above creates an intermediate widget and construct it using its own layout. Than the widget set as a central widget in the MainWindow.