Accessing the functions of custom QWidget class using Signals and Slots - c++

I have a MediaView class which inherits QWidget and has GUI components. And i have another class MediaController which handles the changes made on the GUI components.
I need:
to enable buttons when i receive the signal finishedLoadingFile()
So here is a sample how my class looks:
MediaView.cpp
MediaView::MediaView(QWidget* parent) : QWidget(parent)
{
m_controller = new MediaController(this);
m_btnPlay = new QPushButton(m_mediaButtons);
m_btnPlay->setIcon(QIcon(":/images/play.png"));
m_btnPlay->setIconSize(QSize(32, 32));
m_btnPlay->move(100, 10);
m_btnPlay->resize(39, 38);
m_btnPlay->setToolTip("Play");
m_btnPause = new QPushButton(m_mediaButtons);
m_btnPause->setIcon(QIcon(":/images/pause.png"));
m_btnPause->setIconSize(QSize(32, 32));
m_btnPause->move(150, 10);
m_btnPause->resize(39, 38);
m_btnPause->setToolTip("Pause");
m_btnPlay->setEnabled(false);
m_btnPause->setEnabled(false);
connect(m_btnOpen, SIGNAL(clicked(bool)), m_controller, SLOT(loadFile()));
connect(m_btnPlay,SIGNAL(clicked(bool)),m_controller,SLOT(buttonPlayClicked()));
connect(m_btnPause,SIGNAL(clicked(bool)),m_controller,SLOT(buttonPauseClicked()));
}
void MediaView::enableButtons()
{
m_btnPlay->setEnabled(true);
m_btnPause->setEnabled(true);
}
MediaController.cpp
MediaController::MediaController(QWidget* parent) : QWidget(parent)
{
MediaView* view = new MediaView(this); //loop here
connect(this, SIGNAL(loadingFileFinished(), view, SLOT(enableButtons()));
}
void MediaController::loadFile()
{
m_filePath = "";
m_filePath = QFileDialog::getOpenFileName(this, "Choose a .txt file with paths to gtt streams", QString(),
QString("text file(*.txt)"));
if (m_filePath.isEmpty())
{
return;
}
m_file = "";
m_file.append(m_filePath.toLocal8Bit().constData());
emit finishedLoadingFile();
}
Issue:
I wish to call the enableButtons() in the MediaView class. But when i instantiate the MediaView class inside MediaController, app crashes because they keep calling each others constructors in a loop. Reason being, they both communicate using signals and slots so i need to instantiate objects of their respective classes.
Alternatives:
I would like to know whether if it is possible to access the widgets from MediaController class so that i can use them there itself?
If not, a better way to communicate between view and controller?

Each MediaView creates a MediaController, and each MediaController creates another MediaView, that generates the infinite loop, the appropriate thing is to create a MediaController in MediaView and make the connections:
MediaView::MediaView(QWidget* parent) : QWidget(parent)
{
m_controller = new MediaController(this);
connect(m_controller, &MediaController::loadingFileFinished, this, &MediaView::enableButtons);
[...]
}
it is not necessary to create a new MediaView within MediaController.
If you want to access MediaView from MediaController you can do it through parent since MediaView is the parent of MediaController:
MediaController.h
MediaView *view;
MediaController.cpp
MediaController::MediaController(QWidget* parent) : QWidget(parent)
{
view = qobject_cast<QMediaView *>(parent);
//or
// connect(this, &MediaController::loadingFileFinished, view, &MediaView::enableButtons);
}

Related

Is it possible to call a slot when any of the widgets in a dialog box emits a signal?

I am trying to create a configuration menu box for an application, and have used a QDialog box to display the options that the user can change. This box contains QComboBoxes and QLineEdits, but a lot of them (7 combo boxes and 12 line edits). There is a QPushButton in the bottom called "Apply Changes" that should get enabled only when any property in the box gets changed.
Do I have to link every signal from each widget with a slot to enable the button individually or is there a signal that the QDialog box itself emits when there is a change in its constituent widgets?
Right now I have this:
connect(Combo1,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)));
connect(Combo2,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)))
followed by 17 more lines of these connections.
void MyClass::fnEnable(QString)
{
ApplyButton->setEnabled(true); //It is initialised as false
}
I was wondering if there was a shorter way of doing this, maybe (like I mentioned before) a signal emitted by QDialog (I couldn't find one in the documentation)
I know that this does not speed up the program, as only the required connection is called, but it would make any further attempts at making more ambitious dialog boxes easier.
Actually there is no such signal, but one approach is to create a list of QComboBox, and make the connections with a for, for example:
QList <*QCombobox> l;
l<<combobox1<< combobox2<< ....;
for (auto combo: l) {
connect(combo, &QComboBox::activated, this, &MyClass::fnEnable);
}
The same would be done with QLineEdit.
You can iterate over an initializer list of widgets boxes and leverage C++11 to do all the boring work for you:
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
auto const comboBoxes = {Combo1, Combo2, ... };
for (auto combo : comboBoxes)
connect(combo, &QComboBox::activates, this, &MyClass::fnEnable);
}
You can also automatically find all the combo boxes:
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
ui.setupUi(this); // or other setup code
for (auto combo : findChildren<QComboBox*>(this))
connect(combo, &QComboBox::activated, this, &MyClass::fnEnable);
}
Or you can automatically attach to the user property's change signal. This will work on all controls that have the user property. The user property is the property of a control that contains the primary data the control is displaying.
void for_layout_widgets(QLayout * layout, const std::function<void(QWidget*)> & fun,
const std::function<bool(QWidget*)> & pred = +[](QWidget*){ return true; })
{
if (!layout) return;
for (int i = 0; i < layout->count(); ++i) {
auto item = layout->itemAt(i);
for_layout_widgets(item->layout(), fun, pred);
auto widget = item->widget();
if (widget && pred(widget)) fun(widget);
}
}
class MyClass : public QWidget {
Q_OBJECT
Q_SLOT void MyClass::fnEnable(); // must take no arguments
...
};
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
// setup code here
auto slot = metaObject()->method(metaObject()->indexOfMethod("fnEnable()"));
Q_ASSERT(slot.isValid());
for_layout_widgets(layout(), [=](QWidget * widget){
auto mo = widget->metaObject();
auto user = mo->userProperty();
if (!user.isValid()) return;
auto notify = user.notifySignal();
if (!notify.isValid()) return;
connect(widget, notify, this, slot);
});
}
You can also keep the combo boxes in an array, by value. This minimizes the costs of indirect references and results in code that will take the least amount of memory possible and perform well:
class MyClass : public QWidget {
Q_OBJECT
QVBoxLayout m_layout{this};
std::array<QComboBox, 14> m_comboBoxes;
...
};
MyClass(QWidget * parent) : QWidget(parent) {
for (auto & combo : m_comboBoxes) {
m_layout.addWidget(&combo);
connect(&combo, &QComboBox::activates, this, &MyClass::fnEnable);
}
}

Do an action while any checkbox state is modified with Qt

In my programm, I fill my interface with a lot of checkbox by this way :
void VGCCC::addMaterialToUI(QDomNodeList _materialNodeList, QWidget* _areaWidget, QLayout* _layout, QWidget* _layoutWidget, int _maTable)
{
for(int i=0; i< _materialNodeList.count();i++)
{
QDomElement materialElement = _materialNodeList.at(i).toElement();
QString elementFile = materialElement.attribute("file");
QString elementId = materialElement.attribute("id");
QString elementLabel = elementId;
elementLabel += " - ";
elementLabel += materialElement.attribute("label");
QCheckBox* checkbox = new QCheckBox(elementLabel);
_layout->addWidget(checkbox);
_layoutWidget->adjustSize();
_areaWidget->setMinimumHeight(_layoutWidget->height());
_areaWidget->setMinimumWidth(_layoutWidget->width());
configuration c;
c.path = (m_igmPath+elementFile).toStdString();
c.id = elementId.toInt();
c.name = elementLabel.toStdString();
if(_maTable==0)
{
m_materialSectionMap[checkbox] = c;
}
else
{
m_materialPostMap[checkbox] = c;
}
}
}
I would like to know how to retrieve these "abstract" checkbox. More exactly, if one of these checkbox is checked, I would like to call another function like this :
connect(anyCheckbox,SIGNAL(stateChanged(anyCheckbox)), this, SLOT(doSomethingFunctionIfCheckboxIsChecked()));
The difficulty is that in my UI, these checkbox didn't exist, so I can't connect them to my function. How can I solve it ?
You can e.g. collect pointers to your checkbox objects to a list so can access or "retrieve" them later.
You can connect each checkbox's stateChanged signal to a same slot which is then called when state of any of the checkboxes is changed. In the slot you can cast the sender() to a checkbox if you need to know which specific checkbox is in question. Another alternative is to use QSignalMapper.
In your class declaration:
private slots:
void checkboxStateChanged(int state)
private:
QList<QCheckBox*> m_checkboxes;
In your class definition:
void VGCCC::addMaterialToUI(QDomNodeList _materialNodeList, QWidget* _areaWidget, QLayout* _layout, QWidget* _layoutWidget, int _maTable)
{
...
QCheckBox* checkbox = new QCheckBox(elementLabel);
m_checkboxes.append(checkbox);
connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(checkboxStateChanged(int)));
...
}
void VGCCC::checkboxStateChanged(int state)
{
// Here your can e.g. call doSomethingFunctionIfCheckboxIsChecked()
QCheckBox* checkbox = qobject_cast<QCheckBox*>(sender());
if (checkbox)
{
// checkbox points to the object whose state changed
}
}

Qt: Widget method addButtons() does NOT work as needed [duplicate]

This question already has answers here:
Unable to delete the widgets in sub-layout of a layout in Qt
(3 answers)
Closed 6 years ago.
I am using Qt5 (beginner) on Windows7.
In the main window of my app I want to display and remove some push-buttons.
widget = new ButtonWidget(ui->frame); // frame is a QScrollArea
connect(ui->addBtns, SIGNAL(clicked()), widget, SLOT(addButtons()));
connect(ui->deleteBtns, SIGNAL(clicked()), widget, SLOT(deleteButtons()));
And the ButtonWidget class is here:
ButtonWidget::ButtonWidget(QWidget * parent) : QWidget(parent)
{
//addButtons();
}
void ButtonWidget::addButtons()
{
QStringList texts{"1\nok", "2\nok", "3\nok", "4\nok", "5\nok", "6\nok"};
gridLayout = new QGridLayout;
for(int i = 0; i < texts.size(); i++)
{
QPushButton * button = new QPushButton(texts[i]);
gridLayout->addWidget(button, i / 5, i % 5);
}
setLayout(gridLayout);
}
// I'm not sure this method/function is ok... :(
void ButtonWidget::deleteButtons()
{
QLayoutItem * child;
while((child = gridLayout->takeAt(0)) != 0)
{
gridLayout->removeWidget(child->widget());
delete child->widget();
delete child;
}
delete gridLayout;
}
Problem is: when I click on add_buttons, I get all buttons displayed, but they are shrunk, tiny or something... :
OTOH... if I remove the comment from addButtons() call in the constructor (hence calling from within the constructor), the result is ok:
So, finally I have 2 questions:
1) How to fix the code to be able to add those buttons properly (when add_buttons is clicked)?
2) Is the deleteButtons() method ok?
First, I would recommend to do
gridLayout = new QGridLayout(this);
in the constructor. You don't need to delete all the grid, create it again and set in as a layout when you can just remove its content (with delete button for example) and fill it again afterwards.
EDIT : comment of Werner Erasmus : No need to set the layout. The fact that it has a parent widget implies that it sets itself up.
The problem is that is you do not modify addButtons() you will substitute the previous buttons without knowing where they go.
Also, try to give the QPushButton a parent :
new QPushButton(texts[i],this);
For your second point : Removing widgets from QGridLayout
EDIT:
After some more testing (without looking at the source code, but suspecting that it must be safe to delete button, else things would be brittle), I've implemented removeButtons as follows:
void ButtonWidget::deleteButtons()
{
while(myLayout->count())
{
delete myLayout->itemAt(0)->widget();
}
}
This works, and it proves that deleting a widget also removes the widget from it's parent, and layouts associated with the parent (by slots hooked up to when a child widget is deleted). The above code confirms this, as count(), which refers to number of layout items, decrease to zero (and those layout items are managed by the Layout). takeAt(x) is never called, and doesn't need to be - simply delete the widgets (buttons). Wholla!
ORIGINAL ANSWER
As mentioned in my other post, you only need to delete the buttons (it is removed from its parent automatically). However, if a widget was added to a layout, the parent of that layout becomes the parent of the widget, and an associated QLayoutItem is created that is managed by the layout itself. To the delete the buttons, the safest way is to take all the layout items (ownership the taker's responsibility), delete each items associated widget, and the delete each item. I'll try and find relevant references apart from the sources...
The following code works:
//ButtonWidget.h
#include <QWidget>
#include <QScrollArea>
#include <QHBoxLayout>
class ButtonWidget : public QScrollArea
{
Q_OBJECT
public:
ButtonWidget(QWidget *parent = 0);
~ButtonWidget();
void addButtons();
void deleteButtons();
private:
QHBoxLayout* myLayout;
};
//ButtonWidget.cpp
#include "ButtonWidget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLayoutItem>
ButtonWidget::ButtonWidget(QWidget * parent) :
QScrollArea(parent),
myLayout(new QHBoxLayout(this))
{
}
ButtonWidget::~ButtonWidget()
{
}
void ButtonWidget::addButtons()
{
QStringList texts{"1\nok", "2\nok", "3\nok", "4\nok", "5\nok", "6\nok"};
for(int i = 0; i < texts.size(); i++)
{
myLayout->addWidget(new QPushButton(texts[i]));
}
}
void ButtonWidget::deleteButtons()
{
QLayoutItem * child;
while((child = myLayout->takeAt(0)) != 0)
{
delete child->widget();
delete child;
}
}
#include "ButtonWidget.h"
#include <QApplication>
#include <QScrollArea>
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <memory>
std::unique_ptr<QScrollArea> makeArea()
{
std::unique_ptr<QScrollArea> area(new QScrollArea);
auto layout = new QGridLayout(area.get());
auto addButton = new QPushButton("Add");
auto removeButton = new QPushButton("Remove");
layout->addWidget(addButton, 0, 0);
layout->addWidget(removeButton, 0, 1);
auto btnWidget = new ButtonWidget;
layout->addWidget(btnWidget,1,0,1,2);
QObject::connect(addButton, &QPushButton::clicked, [=]()
{
btnWidget->addButtons();
});
QObject::connect(removeButton, &QPushButton::clicked, [=]()
{
btnWidget->deleteButtons();
});
return move(area);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto area = makeArea();
area->show();
return a.exec();
}
You need to enable c++11 in your config (.pro) to get the lambdas working.
CONFIG += c++11
I've used QHBoxLayout for your buttons, as it better models what you want. Although strictly not necessary, I'm returning unique_ptr from the makeArea in main, as it has not parent, I'm not sure whether it gets some parent because it is the first widget created, but unique_ptr shows intent.
NOTE:
Apparently the layout item is not the parent of the widget, but the widget associated with the layout itself is the parent of widgets belonging to its layout item.

Delete created Qt elements and subclasses

I have a question about Qt. I am wondering how it is I should delete all the pointers I create. For example:
.h file
#ifndef MAINCALENDAR_H
#define MAINCALENDAR_H
#include<QWidget>
#include <QMap>
#include <QComboBox>
#include <QCalendarWidget>
#include <QRadioButton>
#include <QString>
#include <QtGui>
#include "selector.h"
#include <QInputDialog>
class mainCalendar : public QWidget
{
Q_OBJECT
public:
mainCalendar(QWidget * parent = 0);
~mainCalendar();
void showAppointments();
public slots:
void showLCFunc() {select->getTod()->getIntf()->getListClass().orderListChronologic(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
void showLPFunc() {select->getTod()->getIntf()->getListClass().orderListByPriority(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
void loadFile() {QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"", tr("M-Calendar Files (*.mca)"));}
void saveFile() {QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"", tr("M-Calendar Files (*.mca)"));qDebug() << fileName << endl;}
void intshow();
void updater() {if (!Interface->isHidden()) { app->setList(Interface->getList()); app->Beta_update(1,calendar->selectedDate()); }
else if (!TodFace->isHidden()) {tod->setList(TodFace->getList()); tod->Beta_update(1,calendar->selectedDate());} }
private:
QPushButton *showLC;
QPushButton *showLP;
QPushButton *searchButton;
QPushButton *updateButton;
QPushButton *saveButton;
QPushButton *loadButton;
QLabel *instructions;
QPushButton *backButton;
QPushButton *taskButton;
interface * Interface;
todFace * TodFace;
showTod * tod;
showApp * app;
QCalendarWidget *calendar;
QGridLayout *mainLayout;
Selector * select;
};
#endif // MAINCALENDAR_H
.cpp file:
#include "maincalendar.h"
mainCalendar::mainCalendar(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout * footButtons = new QHBoxLayout;
showLC = new QPushButton(tr("'to-do' (chrono)"));
showLP = new QPushButton(tr("'to-do' (priority)"));
searchButton = new QPushButton(tr("&Search"));
saveButton = new QPushButton(tr("&Save calendar"));
loadButton = new QPushButton(tr("&Load Calendar"));
updateButton = new QPushButton(tr("Update"));
footButtons->addWidget(searchButton);
footButtons->addWidget(saveButton);
footButtons->addWidget(loadButton);
footButtons->addWidget(showLC);
footButtons->addWidget(showLP);
instructions = new QLabel(tr("To view or add data, double-click date in calendar"));
calendar = new QCalendarWidget;
calendar->setGridVisible(true);
calendar->setMinimumDate(QDate(2012, 1, 1));
calendar->setMaximumDate(QDate(2016,12,31));
backButton = new QPushButton(tr("&Back to calendar"));
select = new Selector(0,calendar,instructions,backButton, updateButton);
tod = select->getTod();
TodFace = tod->getIntf();
TodFace->hide();
TodFace->setCalendar(calendar);
tod->hide();
app = select->getApp();
Interface = app->getIntf();
Interface->hide();
Interface->setCalendar(calendar);
app->hide();
backButton->hide();
updateButton->hide();
connect(showLC,SIGNAL(clicked()),this,SLOT(showLCFunc()));
connect(showLP,SIGNAL(clicked()),this,SLOT(showLPFunc()));
connect(updateButton, SIGNAL(clicked()), this, SLOT(updater()));
connect(backButton, SIGNAL(clicked()), this, SLOT(intshow()));
connect(loadButton,SIGNAL(clicked()),this,SLOT(loadFile()));
connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));
connect(calendar, SIGNAL(activated(QDate)), this, SLOT(intshow()));
mainLayout = new QGridLayout;
this->setMinimumHeight(800);
this->setMinimumWidth(1000);
mainLayout->setColumnMinimumWidth(0,500);
mainLayout->addWidget(calendar,0,0);
mainLayout->addWidget(app,1,0);
mainLayout->addWidget(Interface,1,2);
mainLayout->addWidget(tod,1,0);
mainLayout->addWidget(TodFace,1,2);
mainLayout->addWidget(backButton,0,0,Qt::AlignTop);
mainLayout->addLayout(footButtons,2,0,Qt::AlignLeading);
mainLayout->addWidget(instructions,2,0,Qt::AlignTrailing);
mainLayout->addWidget(updateButton,2,2,Qt::AlignRight);
setLayout(mainLayout);
setWindowTitle(tr("M-Calendar"));
}
mainCalendar::~mainCalendar()
{
}
void mainCalendar::intshow()
{
if (Interface->isHidden()&&TodFace->isHidden())
{
select->setDate(calendar->selectedDate());
select->show();
Interface->setdate(calendar->selectedDate());
TodFace->setdate(calendar->selectedDate());
} else
{
backButton->hide();
updateButton->hide();
Interface->hide();
app->close();
TodFace->hide();
tod->close();
calendar->show();
instructions->show();
}
}
I am stuck here. Am I supposed to do to delete all the pointers (QPushbutton, etc) and subclasses so no memory leaks occur?
Short answer: No, you don't have to explicitly delete them.
In Qt, the QObjects are organized in object trees. The parent-child relationships of the various widgets also implies that the parent takes ownership of the child widgets.
As a result you do not have to explicitly delete them when your application finishes. Each parent will take care of the cleanup of its own children. Only when you create a (pointer to) a widget/object which has no parent, you will need to explicitly delete it.
via addWidget you create a parent-child-relationship between these to widgets.
Any parentless widget has to be destroyed manually, which in turn free its children recursivly.
As long as your widget has a parent, and that is either freed manually or child to another Widget, no manual free is required.
Same accounts for QObject derived classes.

how to connect a QRadioButton to a QTabWidget?

i am creating a software using C++ and QT,and i have two widget one of type QRadioButon and one QTabWidget. My need is that i want to send signal from a radio button and i want that whenever the button is checked the content of tab get changed.
Can anyone suggest how to do that? I tried creating a slot of my widget class and in that slot i called the constructor of tab class but the problem is the construtor is not getting called.
here is the code i am using..
#include <QtGui>
#include "v_lab.h"
v_lab::v_lab(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("Virtual Lab");
maingroup=new QGroupBox(this);
maingroup->setTitle("Algorithms");
maingroup->setMinimumWidth(200);
maingroup->setMaximumWidth(240);
maingroup->setFlat(false);
p=new QPalette;
p->setColor(QPalette::Background,QColor(233,212,102));
setPalette(*p);
box=new QGroupBox(maingroup);
box->setFlat(false);
box->setTitle("Searching Algorithm");
linear_search=new QRadioButton("Linear Search",box);
linear_search->setChecked(1);
binary_search=new QRadioButton("Binary Search",box);
box1=new QGroupBox(maingroup);
box1->setFlat(false);
box1->setTitle("Sorting Algorithms");
bubble_sort=new QRadioButton("Bubble Sort",box1);
selection_sort=new QRadioButton("Selection Sort",box1);
box2=new QGroupBox(maingroup);
box2->setFlat(false);
box2->setTitle("Tree Algorithms");
infix_traversal=new QRadioButton("Infix Traversal",box2);
prefix_traversal=new QRadioButton("Prefix Traversal",box2);
postfix_traversal=new QRadioButton("Postfix Traversal",box2);
box3=new QGroupBox(maingroup);
box3->setFlat(false);
box3->setTitle("Graph Algorithms");
bfs=new QRadioButton("BFS",box3);
dfs=new QRadioButton("DFS",box3);
shortest_path=new QRadioButton("Shortest Path",box3);
QString string1="go to hell";
tab=new QTabWidget;
tab->addTab(new algorithm(string1),"Algorithm");
// tab->addTab(new psudo_code(),"Pseduo-Code");
tab->setMinimumWidth(250);
tab->setMaximumWidth(400);
//Layout
mainlayout=new QHBoxLayout(this);
mainlayout->addWidget(maingroup);
mainlayout->addWidget(tab);
mainlayout->addStretch();
main_left_pane_layout=new QVBoxLayout(maingroup);
main_left_pane_layout->addWidget(box);
main_left_pane_layout->addWidget(box1);
main_left_pane_layout->addWidget(box2);
main_left_pane_layout->addWidget(box3);
left_pane_box=new QVBoxLayout(box);
left_pane_box->addWidget(linear_search);
left_pane_box->addWidget(binary_search);
left_pane_box1=new QVBoxLayout(box1);
left_pane_box1->addWidget(bubble_sort);
left_pane_box1->addWidget(selection_sort);
left_pane_box2=new QVBoxLayout(box2);
left_pane_box2->addWidget(infix_traversal);
left_pane_box2->addWidget(prefix_traversal);
left_pane_box2->addWidget(postfix_traversal);
left_pane_box3=new QVBoxLayout(box3);
left_pane_box3->addWidget(bfs);
left_pane_box3->addWidget(dfs);
left_pane_box3->addWidget(shortest_path);
connect(binary_search,SIGNAL(clicked()),this,SLOT(peeyush()));
}
algorithm::algorithm(const QString &string,QWidget *parent)
: QWidget(parent)
{
label=new QLabel(string);
main_layout=new QVBoxLayout;
main_layout->addWidget(label);
main_layout->addStretch();
setLayout(main_layout);
}
/*
psudo_code::psudo_code(QWidget *parent)
: QWidget(parent)
{
label1=new QLabel("Hello Peeyush Chandel");
main_layout1=new QVBoxLayout;
main_layout1->addWidget(label1);
main_layout1->addStretch();
setLayout(main_layout1);
}
*/
void v_lab::peeyush()
{
QString string1="new string";
algorithm obj(string1);
//exit(1);
}
In the header definition file of your v_lab class you should have something like this:
// Includes here.
class v_lab: public QDialog
{
Q_OBJECT // VERY important!
public:
// Other things here.
private slots: // VERY important. You can use public slots too.
void peeyush();
}
And you cannot connect a signal to a constructor.