How can QAction ask for input when triggered? - c++

I am trying to have a little OpenGL drawing application which needs files. I need to open a file using the menu bar. What I want is, when a user triggers the action, there is a little popup window that allows the user to enter the input.
Is is possible to do so using Qt? If yes, how?
glmainwindow.cpp
#include "glmainwindow.h"
#include <QGroupBox>
#include <QMenuBar>
glMainWindow::glMainWindow(fileReader reader, QWidget *parent) : QMainWindow(parent)
{
// initialization(reader);
QGroupBox *box = new QGroupBox(this);
mainLayout = new QGridLayout();
glWidget = new mainWidget(reader.p1, reader.p2);
mainLayout->addWidget(glWidget, 0, 0); //glWindow, 0, 0); //instance, 0, 0); //glWindow, 0, 0); //game, 1, 0); //simpleTex, 0, 0); //cubeTextureWindow, 0, 0);
/* Above FOR simpleGame */
userInput = new QLineEdit;
mainLayout->addWidget(userInput, 1, 0);
box->setLayout(mainLayout);
setCentralWidget(box);
setGeometry(150, 200, 720, 740);
createActions();
createMenus();
}
void glMainWindow::createMenus()
{
glMenuBar = menuBar();
fileMenu = new QMenu("File", this);
fileMenu->addAction(openFileAction);
fileMenu->addAction(closeAction);
glMenuBar->addMenu(fileMenu);
}
void glMainWindow::createActions()
{
openFileAction = new QAction(tr("Open file"), this);
// connect(openFileAction, &QAction::triggered)
closeAction = new QAction("Exit", this);
connect(closeAction, &QAction::triggered, glWidget, &QWidget::close);
}
glMainWindow.h
#ifndef GLMAINWINDOW_H
#define GLMAINWINDOW_H
#include <QPushButton>
#include <QLabel>
#include <QMainWindow>
#include <QGridLayout>
#include <QSlider>
#include <QLineEdit>
#include <QAction>
#include "../roadsFileRead/filereader.h"
#include "mainwidget.h"
class glMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit glMainWindow(fileReader reader, QWidget *parent = nullptr);
private:
void createMenus();
void createActions();
private:
QGridLayout *mainLayout;
mainWidget *glWidget{nullptr};
QSlider* xSlider;
QSlider* ySlider;
QSlider* zSlider;
QLineEdit *userInput;
QMenuBar *glMenuBar;
QMenu *fileMenu;
QAction *closeAction{nullptr};
QAction *openFileAction{nullptr};
};
#endif // GLMAINWINDOW_H
I have tried searching online and similar stackoverflow questions, but to no avail. However, I have seen some applications do this. I found similar tutorials, but they didn't have anything that's like what I want. How can I connect this to the triggering of the action? Also, I am not using Qt Designer.

you are almost there... just add a QfileDialog as suggested in the comments..
void glMainWindow::createActions()
{
//define the object for the file name:
QString fileName = "";
openFileAction = new QAction(tr("Open file"), this);
connect(openFileAction, &QAction::triggered, []()
{
fileName = QFileDialog::getOpenFileName(this,
tr("Open the file"), "/home/user/path", tr("my Files (*.txt *.csv)"));
});
....
}

So I got my own way. I used QInputDialog NOT QFileDialog, as QFileDialog doesn't help and is confusing. I added this to glmainwindow.h:
void openFileAct()
{
QString filePath = QInputDialog::getText(0, "File path",
"FIle path", QLineEdit::Normal,
"");
openFile(filePath.toStdString());
}
where openFile is the function that opens the file. I connected the action to openFileAct.

Related

How do I get values from multiple QLineEdit into one matrix?

I'm quite new to QT, and I wanted to do the following:
Have a layout which initially consist of 1 QLineEdit (text) for input and 1 button for confirming it (button).
Then I wanted to send signal SetN(N) when the button is clicked and this signal should activate ArrayEdit(N) slot, which will change the layot to have N inputs (QLineEdit), and 1 button to send what's in them (as one array) to further processing .
I managed to do the first part, but... it didn't work and I don't know how to deal with this no matching member function error
Here's code of my class:
#include "textlayout.h"
TextLayout::TextLayout()
{
setWindowTitle("Choosing N value");
resize(200, 200);
auto layout = new QGridLayout(this);
auto text = new QLineEdit;
text->resize(10, 30);
auto button = new QPushButton();
button->setText("set");
layout->addWidget(text, 0, 0);
layout->addWidget(button, 0, 1);
QObject::connect(&button, SIGNAL(SetN())„ this, SLOT(ArrayEdit())); //no matching member function for call to 'connect'
int N = text->text().toInt();
if(N > 0)
{
emit SetN(N);
}
}
And its header file:
#ifndef TEXTLAYOUT_H
#define TEXTLAYOUT_H
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QObject>
class TextLayout : public QWidget
{
Q_OBJECT
public:
TextLayout();
public slots:
void ArrayEdit(int N);I
signals:
void SetN(int N);
};
#endif // TEXTLAYOUT_H
I know that actually now I don't activate this signal on click, but... well previously I didn't realize that and I don't know how to have sent parameter with onclick signal...
How can i work around it and how to fix this class?
you are not far away from doing it.. let me help you a little :)
TextLayout::TextLayout()
{
setWindowTitle("Choosing N value");
resize(200, 200);
/*auto*/ layout = new QGridLayout(this);
//declare layout in header, cos you will need it later in the slot
auto text = new QLineEdit;
text->resize(10, 30);
auto button = new QPushButton();
button->setText("set");
layout->addWidget(text, 0, 0);
layout->addWidget(button, 0, 1);
//QObject::connect(&button, SIGNAL(SetN())„ this, SLOT(ArrayEdit())); //no matching member function for call to 'connect'
//you dont need the QObject cos Textlayout is a QObject so just call connect:
connect(this, &TextLayout::SetN, this, &TextLayout::ArrayEdit);
//SetN is not a signal of QButton so you cant connect that like that... instead
connect(&button, &QPushButton::clicked, [this]()
{
int N = text->text().toInt();
if(N > 0)
{
emit SetN(N);
}
});
}
If you handle numeric values use QSpinBox instead of QLineEdit. Then you can also set limits and the user can't input invalid data.
Use QLineEdit for text.
Widget.hpp
#pragma once
#include <QWidget>
class QGridLayout;
class QLineEdit;
class QPushButton;
class QSpinBox;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void adjustLayout(int n);
private:
QGridLayout* _layout {};
QPushButton* _button {};
QSpinBox* _spinBox {};
QVector<QLineEdit*> _lineEditVector;
};
Widget.cpp
#include "Widget.hpp"
#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
_layout = new QGridLayout(this);
_spinBox = new QSpinBox;
_button = new QPushButton("set");
_layout->addWidget(_spinBox, 0, 0);
_layout->addWidget(_button, 0, 1);
connect(_button, &QPushButton::clicked,
this, [this](bool) {
emit adjustLayout(_spinBox->value());
});
}
Widget::~Widget() = default;
void Widget::adjustLayout(int n)
{
for (int i=0; i<n; ++i)
{
QLineEdit* lineEdit = new QLineEdit(QString::number(i+1));
// for later access, keep a reference in the vector:
_lineEditVector.push_back(lineEdit);
_layout->addWidget(lineEdit, i + 1, 0);
}
}

Qt: SIGNAL and SLOT don't work in a custom QDialog window

I have an application in which I want to prompt a dialog window for further (advanced) editing of apps content. It has to be a modal window, so that's why it can't be a QMainWindow. I just can't seem to make connect() macro work in QDialogwindow.
Previously, I got around this by using the response of the QDialog and a getter function in my dialog class:
A screenshot of a simple query
Dialog_CreateNew mDialog(this);
mDialog.setModal(true);
if (mDialog.exec() == QDialog::Accepted)
{
QString username, password;
mDialog.getData(&username, &password);
}
I used built-in SLOTs of QDialog library accept() and reject():
connect(_cancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(_createNew, SIGNAL(clicked()), this, SLOT(accept()));
But now when I have to create my own slots in the dialog window, idk how to make it work. It will be a complex window, and just accept() and reject() won't do it.
One more thing: when I add Q_OBJECT macro in the dialog class, I get an error:
error: undefined reference to `vtable for Dialog_CreateNew'
These built-in SLOTs accept() and reject() work without Q_OBJECT.
I've seen this work in the Qt Designer, therefore it is possible. I don't want to use the Designer, everything ought to be done in coding.
That's my research and the things I tried.
My question is: How to make a signal-slot mechanism work in a modal child dialog window?
"dialog_createNew.h":
#ifndef DIALOG_CREATENEW_H
#define DIALOG_CREATENEW_H
#include <QtCore>
#include <QDialog>
#include <QIcon>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QStatusBar>
class Dialog_CreateNew : public QDialog
{
Q_OBJECT
public:
Dialog_CreateNew(QWidget* parent);
void getData(QString* username, QString* password);
void setErrorTip(QString errorTip);
virtual ~Dialog_CreateNew();
private:
QWidget* _parent;
QLabel* _lInstruction;
QLabel* _lUsername;
QLabel* _lPassword;
QLineEdit* _edUsername;
QLineEdit* _edPassword;
QPushButton* _createNew;
QPushButton* _cancel;
QLabel* _lErrorTip;
QString* _errorTip;
QGridLayout* _layout;
void createConnects();
private slots:
void onTextChanged_connect(QString);
};
#endif // DIALOG_CREATENEW_H
"dialog_createNew.cpp":
#include "dialog_createnew.h"
Dialog_CreateNew::Dialog_CreateNew(QWidget* parent)
{
_parent = parent;
this->setWindowTitle("Create New Bot");
this->setWindowIcon(QIcon(":/icons/createNew.svg"));
int nHeight = 150;
int nWidth = 360;
this->setGeometry(parent->x() + parent->width()/2 - nWidth/2,
parent->y() + parent->height()/2 - nHeight/2,
nWidth, nHeight);
this->setFixedSize(QSize(nWidth, nHeight));
_lInstruction = new QLabel(this);
_lInstruction->setText("Enter Your Instagram credentials:");
_lUsername = new QLabel(this);
_lUsername->setText("Username:");
_lPassword = new QLabel(this);
_lPassword->setText("Password:");
_edUsername = new QLineEdit(this);
_edUsername->setPlaceholderText("classybalkan");
_edPassword = new QLineEdit(this);
_edPassword->setEchoMode(QLineEdit::Password);
_edPassword->setPlaceholderText("•••••••••••");
_createNew = new QPushButton(this);
_createNew->setText("Create New Bot");
_cancel = new QPushButton(this);
_cancel->setText("Cancel");
_errorTip = new QString("");
_lErrorTip = new QLabel(this);
_layout = new QGridLayout(this);
_layout->addWidget(_lInstruction, 0, 0, 1, 2);
_layout->addWidget(_lUsername, 1, 0);
_layout->addWidget(_edUsername, 1, 1);
_layout->addWidget(_lPassword, 2, 0);
_layout->addWidget(_edPassword, 2, 1);
_layout->addWidget(_lErrorTip, 3, 1);
_layout->addWidget(_cancel, 4, 0);
_layout->addWidget(_createNew, 4, 1);
this->setLayout(_layout);
createConnects();
}
void Dialog_CreateNew::createConnects()
{
connect(_cancel,
SIGNAL(clicked()),
this,
SLOT(reject()));
connect(_edPassword,
&QLineEdit::textChanged,
this,
&Dialog_CreateNew::onTextChanged_connect);
}
void Dialog_CreateNew::getData(QString* username, QString* password)
{
*username = _edUsername->text();
*password = _edPassword->text();
}
void Dialog_CreateNew::setErrorTip(QString errorTip)
{
*_errorTip = errorTip;
_lErrorTip->setText("<center><font color=""red"">"
+ *_errorTip +
"</font><center>");
}
void Dialog_CreateNew::onTextChanged_connect()
{
QString text = _edPassword->text();
if (text == "")
{
disconnect(_createNew,
&QPushButton::clicked,
this,
&QDialog::accept);
_lErrorTip->setText("Invalid Password");
}
else
{
connect(_createNew,
&QPushButton::clicked,
this,
&QDialog::accept);
}
}
Dialog_CreateNew::~Dialog_CreateNew()
{
}
Solution:
Changing old SIGNAL/SLOT notation with the new function binding:
connect(_edPassword, &QLineEdit::textChanged, this, &Dialog_CreateNew::onTextChanged_connect);
This allowed me to use my own slots, and did the fix.
Three things you should do to fix this:
Split your work to header (.h file) and source (.cpp file)
Define the destructor of your dialog as virtual, e.g.:
virtual ~MyDialog()
{
}
Run qmake again
One or more of these will fix it for you
PS: Please, oh, Please... stop using the outdated SIGNAL() and SLOT(), and start using the new format that uses function binding, like:
connect(_cancel, &QPushButton::clicked, this, &QDialog::reject);
It's faster, more efficient, doesn't use strings, and once compiled, works. Unlike the old format that can fail at runtime.

True content is not copied from one QMap into another QMap

When I press the close button on the tab widget, that tab must close, and the corresponding member of the identifyedWidgetMap must be copied into the deletedWidgetMap. However, the copying does not happen.
For instance, I close Tab_3 but there is in the deletedWidgetMap some information: 4, QWidget(0x17424a1).
Help if you can. Thank you.
That code lines I created are located below.
Header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QTextEdit>
#include <QMap>
#include <QList>
#include <iostream>
#include <QDebug>
#include <QString>
#include <QFont>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setMainWindowPropertiesAndItems();
QMenuBar* menuBar;
QMenu* fileMenu;
QAction* newTabAction;
QAction* openAction;
QTabWidget* tabWidget;
QMap<int, QWidget*> identifyedWidgetMap;
QMap<int, QWidget*> deletedWidgetMap;
QList<QTextEdit*> textEditList;
QVBoxLayout* vBoxLayout;
private slots:
void newTabActionHandler();
void openActionHandler();
void tryingToCloseConcreteTab(int);
};
#endif // MAINWINDOW_H
Source
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMainWindowPropertiesAndItems();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setMainWindowPropertiesAndItems()
{
setWindowTitle("Notepad");
setGeometry(0, 0, 500, 250);
move(270, 270);
menuBar = new QMenuBar(this);
setMenuBar(menuBar);
fileMenu = new QMenu("&File", this);
menuBar->addMenu(fileMenu);
newTabAction = new QAction("&New Tab", this);
fileMenu->addAction(newTabAction);
connect(newTabAction, SIGNAL(triggered()), this,
SLOT(newTabActionCommandsHandler()));
openAction = new QAction("&Open", this);
fileMenu->addAction(openAction);
connect(openAction, SIGNAL(triggered()), this,
SLOT(openActionCommandsHandler()));
identifyedWidgetMap.insert(0, new QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget = new QTabWidget(this);
tabWidget->addTab(identifyedWidgetMap.value(0), QString("Tab
%1").arg(identifyedWidgetMap.size()-1));
tabWidget->setMovable(true);
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this,
SLOT(tryingToCloseConcreteTab(int)));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(0)->setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[0]);
textEditList[0]->setCurrentFont(QFont("Monospace Regular", 14));
setCentralWidget(tabWidget);
}
void MainWindow::newTabActionCommandsHandler()
{
if(bool(deletedWidgetMap.isEmpty()) == true)
{
identifyedWidgetMap.insert(identifyedWidgetMap.size(), new
QWidget(this));
textEditList.append(new QTextEdit(this));
tabWidget->
addTab(identifyedWidgetMap.value(identifyedWidgetMap.size()-1),
QString("Tab %1").arg(identifyedWidgetMap.size()-1));
tabWidget->
setCurrentWidget(identifyedWidgetMap.
value(identifyedWidgetMap.size()-1));
vBoxLayout = new QVBoxLayout();
identifyedWidgetMap.value(identifyedWidgetMap.size()-1)->
setLayout(vBoxLayout);
vBoxLayout->addWidget(textEditList[textEditList.size()-1]);
textEditList[textEditList.size()-1]->setCurrentFont(QFont("Monospace
Regular", 14));
}
}
void MainWindow::openActionCommandsHandler()
{
QWidget* currentWidget = tabWidget->currentWidget();
std::cout<<"The currentWidget address is: "<<currentWidget<<std::endl;
qDebug()<<"The complete identifyedWidgetMap address set is: ";
foreach(int widgetIdentifyer, identifyedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<identifyedWidgetMap.value(widgetIdentifyer);
}
int currentWidgetIndex = 0;
currentWidgetIndex = tabWidget->currentIndex();
std::cout<<"The currentWidgetIndex is: "<<currentWidgetIndex<<std::endl;
qDebug()<<"The complete deletedWidgetMap set is: ";
foreach(int widgetIdentifyer, deletedWidgetMap.keys())
{
qDebug()<<widgetIdentifyer<<", "
<<deletedWidgetMap.value(widgetIdentifyer);
}
}
void MainWindow::tryingToCloseConcreteTab(int concreteIndex)
{
tabWidget->removeTab(concreteIndex);
std::cout<<"Deleted identifyer is: "<<concreteIndex<<std::endl;
deletedWidgetMap.insert(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex)),
identifyedWidgetMap.value(identifyedWidgetMap.key(tabWidget-
>widget(concreteIndex))));
}
The problem I see is you first remove the tab from the tabWidget. This results in an index change, the next tab will shift to left when you remove the tab. When you try to access the tab with tabWidget->widget(concreteIndex)you access the next tab.
You may consider adding tab to the deletedWidgetMap before removing it.

QFileDialog How to set filename to the text field and use QFileDialog separately with few textfields

I'm trying to write GUI for a program that must perform some actions with given files. Its logic will be:
1) Program starts with one text field and one button created.
2) If I click on button, I can choose some .exe file. If the file is choosen, its path is set to the textfield that is logicaly linked with first my button.
3) If the file is choosen on previous step, a new pair of text field and button linked to it is created. The size of main window must change dynamically when a new pair is added.
How can I set the path to the file to current textfield?
I need a possibility to edit data in any text field. How to organize interface so that I could separately use QFileDialog with any pair of textfield and button.
I cant figure out how to use signals/slots in this case.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile();
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
qDebug() << fileName;
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), this, SLOT(openFile()));
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}
You should use QSignalMapper for this.
Your code may looks like this:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSignalMapper>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void makeInterface();
private slots:
void openFile(QWidget* widget);
private:
QSignalMapper _mapper;
};
#endif
#include <QString>
#include <QFileDialog>
#include <QDebug>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
connect(&_mapper, SIGNAL(mapped(QWidget*)), this, SLOT(openFile(QWidget*)));
makeInterface();
}
MainWindow::~MainWindow() {}
void MainWindow::openFile(QWidget* widget)
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("OpenFile"),
QDir::currentPath(),
tr("Executable Files (*.exe)"));
if (!fileName.isNull())
{
static_cast<QLineEdit*>(widget)->setText(fileName);
}
}
void MainWindow::makeInterface()
{
QGridLayout *mainLayout = new QGridLayout;
QLineEdit *fldFilePath = new QLineEdit;
QPushButton *btnOpenFile = new QPushButton("*.exe");
connect(btnOpenFile, SIGNAL(clicked()), &_mapper, SLOT(map()));
_mapper.setMapping(btnOpenFile, fldFilePath);
mainLayout->addWidget(fldFilePath, 0, 0);
mainLayout->addWidget(btnOpenFile, 0, 1);
QPushButton *btnBuild = new QPushButton("Build");
mainLayout->addWidget(btnBuild, 5, 0);
setLayout(mainLayout);
}

Qt Catching signal of checkbox to do an action

In my Qt program, I want to catch the signal of one of my checkbox to know if it's checked or not. I did it :
#include <iostream>
#include <QVBoxLayout>
#include <QtGui>
#include "Window.h"
Window::Window() : QWidget()
{
setFixedSize(400, 800);
m_bouton = new QPushButton("Module 1");
m_bouton->setCursor(Qt::PointingHandCursor);
m_bouton2 = new QPushButton("Module 2");
m_bouton2->setCursor(Qt::PointingHandCursor);
m_bouton3 = new QPushButton("Module 3");
m_bouton3->setCursor(Qt::PointingHandCursor);
m_bouton4 = new QPushButton("Module 4");
m_bouton4->setCursor(Qt::PointingHandCursor);
m_check4 = new QCheckBox(this);
m_check4->move(300, 50);
m_check4->setChecked(true);
m_check3 = new QCheckBox(this);
m_check3->move(200, 50);
m_check3->setChecked(true);
m_check2 = new QCheckBox(this);
m_check2->move(100, 50);
m_check2->setChecked(true);
m_check1 = new QCheckBox(this);
m_check1->move(0, 50);
m_check1->setChecked(true);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_bouton);
layout->addWidget(m_bouton2);
layout->addWidget(m_bouton3);
layout->addWidget(m_bouton4);
this->setLayout(layout);
this->setStyleSheet("border: 1px solid black; border-radius: 10px;");
this->setWindowOpacity(0.5);
QWidget::setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
QWidget::setWindowTitle("Monitor Core");
QObject::connect(m_bouton4, SIGNAL(clicked()), this, SLOT(SizeCHange()));
QObject::connect(m_check4, SIGNAL(clicked()), this, SLOT(SizeCHange()));
}
void Window::SizeCHange()
{
setFixedSize(400, 800 - 200);
m_bouton4->setVisible(false);
}
and this is my .h :
#ifndef _WINDOW_H_
#define _WINDOW_H_
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QCheckBox>
class Window : public QWidget
{
Q_OBJECT
public:
Window();
public slots:
void SizeCHange();
private:
QPushButton *m_bouton;
QPushButton *m_bouton2;
QPushButton *m_bouton3;
QPushButton *m_bouton4;
QCheckBox *m_check1;
QCheckBox *m_check2;
QCheckBox *m_check3;
QCheckBox *m_check4;
};
#endif
This is working but just one time. I want to know if it's check or not, then I will be able to resize my window and make something invisible as I want.
You simply need to use a bool parameter, which indicates the state of the checkbox:
public slots:
void SizeCHange(bool checked); //using bool parameter
//...
QObject::connect(m_check4, SIGNAL(clicked(bool)), this, SLOT(SizeCHange(bool)));
//...
void Window::SizeCHange(bool checked)
{
if(checked)
{
setFixedSize(400, 800 - 200);
}
else
{
// removing fixed size
setMinimumSize(QSize(0, 0));
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
}
m_bouton4->setVisible(checked);
}
The docs for a signal clicked()