change text of QLabel in another class with button - qt

I have ever post a question at there: change text in another class by button But it justs working when second class is create by first class. Now I have 2 classes, they're created sametime, so how to I connect with together? below is all my code, first class has a button, and second class has a label, I want to when user click on the button in first class, label in second class will be changed. They're put in stackWidget:
// file.h
#include <QWidget>
#include <QtGui>
class widgetA;
class widgetB;
class A : public QWidget
{
Q_OBJECT
public:
explicit A(QWidget *parent = 0);
private:
QComboBox* comboBox;
QStackedWidget* stackWidget;
widgetA *wa;
widgetB *wb;
};
class widgetA : public QWidget
{
Q_OBJECT
public:
widgetA(QWidget *parent = 0);
public slots:
void buttonClicked();
private:
QPushButton* button;
};
class widgetB : public QWidget
{
Q_OBJECT
public slots:
void labelChangeText(const QString);
public:
widgetB(QWidget *parent = 0);
QLabel* label;
};
And this's cpp file:
//file.cpp
#include "a.h"
A::A(QWidget *parent) :
QWidget(parent)
{
comboBox = new QComboBox(this);
comboBox->addItem(tr("Widget A"));
comboBox->addItem(tr("Widget B"));
wa = new widgetA(this);
wb = new widgetB(this);
stackWidget = new QStackedWidget(this);
stackWidget->addWidget(wa);
stackWidget->addWidget(wb);
stackWidget->setCurrentIndex(0);
connect(comboBox, SIGNAL(currentIndexChanged(int)), stackWidget, SLOT(setCurrentIndex(int)));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(comboBox);
layout->addWidget(stackWidget);
setLayout(layout);
}
widgetA::widgetA(QWidget *parent):
QWidget(parent)
{
button = new QPushButton(tr("Click"));
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(button);
setLayout(lay);
}
void widgetA::buttonClicked()
{
// what I have to do at there for call the function at widgetB class?
}
widgetB::widgetB(QWidget *parent):
QWidget(parent)
{
label = new QLabel("....");
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(label);
setLayout(lay);
}
void widgetB::labelChangeText(const QString text)
{
label->setText(text);
}
P/S: sorry my english

You can either pass a pointer of widgetB object to widgetA, or you can connect a signal from widgetA to a slot in widgetB.
Here's an example of the signal/slot option:
// widgeta.h
signals:
void changeText(QString text);
-
// widgeta.cpp
void widgetA::buttonClicked()
{
emit changeText("button clicked");
}
-
//widgetb.h
public slots:
void labelChangeText(const QString & text);
-
// a.cpp
connect(wa, SIGNAL(changeText(QString)), wb, SLOT(labelChangeText(QString)));

What hinders you to call wb->labelChangeText("some string") in widgetA::buttonClicked()? You have a pointer to widgetB in class A. And labelChangeText is public in class B. Just that it is a slot does not mean that you cannot call it as a normal method.

Related

How to install event filter on a custom Qt class?

I want to install an event filter on an object of a custom class in Qt. So I created a project such as QtGuiApplication1 on the Qt Designer and created a simple class as myClass as which has a widget and a QGraphicsView for drawing a colored rectangle.
in header file:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include "myClass.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication1Class ui;
bool eventFilter(QObject *obj, QEvent *ev);
myClass* myClass_;
};
in .cpp
#include "QtGuiApplication1.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
myClass_ = new myClass(this, QRect(100, 100, 200, 200));
myClass_->installEventFilter(this);
}
bool QtGuiApplication1::eventFilter(QObject * obj, QEvent * ev)
{
if (obj == myClass_)
{
bool hi = true;
}
return false;
}
and the myClass code is here:
header file of myClass:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QGraphicsView>
class myClass : public QObject
{
Q_OBJECT
public:
explicit myClass(QObject *parent = 0);
myClass();
myClass(QWidget* parent, QRect inRect);
private:
QWidget * widget;
QGraphicsView* qGraph_back;
QGraphicsScene* scene_back;
};
#endif /
cpp file of myClass:
#include "myClass.h"
myClass::myClass(QObject *parent) :
QObject(parent)
{
}
myClass::myClass()
{
}
myClass::myClass(QWidget* parent, QRect inRect)
{
widget = new QWidget(parent);
qGraph_back = new QGraphicsView(widget);
scene_back = new QGraphicsScene(qGraph_back);
widget->setGeometry(inRect);
scene_back->setSceneRect(0,0,inRect.width(),inRect.height());
qGraph_back->setBackgroundBrush(QColor(0, 0, 255, 80));
qGraph_back->setScene(scene_back);
qGraph_back->show();
}
I want to get all the events of myClass_ object such as mouse event, But I can't and the eventfilter doesn't work. how to install eventfilter on the object?
The event filter will work only for events in your MyClass instance, only. Not for its children.
So, events, such as a mouse click or move, in your qGraph_back will be not visible in your eventFilter method.
When you add a child in a widget, an QChildEvent event is raised. You can use it to install the event filter on all children (and grandchildren, etc.). But, you have to install the event filter on your MyClass before adding the children.
A quick example:
class Listener: public QObject
{
public:
Listener(): QObject()
{}
bool eventFilter(QObject* object, QEvent* event)
{
qDebug() << Q_FUNC_INFO << object << event;
if (event->type() == QEvent::ChildAdded)
{
QChildEvent* ev = dynamic_cast<QChildEvent*>(event);
ev->child()->installEventFilter(this);
}
return false;
}
};
class Widget: public QWidget
{
public:
Widget(QObject* parent) : QWidget()
{
installEventFilter(parent);
QGraphicsView* view = new QGraphicsView(this);
auto layout = new QHBoxLayout(this);
layout->addWidget(view);
layout->addWidget(new QLabel("Foobar"))
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Listener* listener = new Listener();
Widget* w = new Widget(listener);
w->show();
return app.exec();
}
As you can see, the events in the QLabel are now sent to the listener. But, you can't see the events from the view because they are caught by the viewport widget in the QGraphicsView...
You have to handle the case where the added child has a viewport (inherits from QAbstractItemView, etc.) and it becomes more complicated.
So, if you have to know when the user clicks on your view, it would be easier to use signals/slots and not an event filter.

how to make a widget of multiple other widget in QT

I am new in Qt and am using qt creator for the GUI designing, now i have to make a dynamic form in which a label,line edit and a button are added for each feature like length,width,etc.However the features are not static there may be 2 feature sometime and other time there might be 6 depending upon the xml file.thus i want to make a widget which for each feature creates an instance.
I have made basic structure of grouped widget using this:
cal_widget.h
#ifndef CAL_WIDGET_H
#define CAL_WIDGET_H
#include <QWidget>
#include<QVBoxLayout>
#include<QPushButton>
#include<QLineEdit>
class cal_widget : public QWidget
{
Q_OBJECT
public:
explicit cal_widget(QWidget *parent = nullptr);
QVBoxLayout* layout;
QPushButton* btn;
QLineEdit* ln1;
QLineEdit* ln2;
signals:
public slots:
};
#endif // CAL_WIDGET_H
cal_widget.cpp
#include "cal_widget.h"
cal_widget::cal_widget(QWidget *parent) : QWidget(parent)
{
layout = new QVBoxLayout();
btn= new QPushButton();
ln1 = new QLineEdit("mm");
ln2 = new QLineEdit("pix");
layout->addWidget(ln1);
layout->addWidget(btn);
layout->addWidget(ln2);
this->setLayout(layout);
}
and calling the above widget in another form mainwidget.cpp
{
QVBoxLayout* vbox = new QVBoxLayout();
cal_widget* cal1 = new cal_widget(this);
cal_widget* cal2 = new cal_widget(this);
cal_widget* cal3 = new cal_widget(this);
cal_widget* cal4 = new cal_widget(this);
cal_widget* cal5 = new cal_widget(this);
vbox->addWidget(cal1);
vbox->addWidget(cal2);
vbox->addWidget(cal3);
vbox->addWidget(cal4);
vbox->addWidget(cal5);
ui->scrollArea->setLayout(vbox);
}
i am getting result as :this
how can i get desired result
DONE
cal_widget.h
#ifndef CAL_WIDGET_H
#define CAL_WIDGET_H
#include <QWidget>
#include<QGroupBox>
#include<QGridLayout>
#include<QPushButton>
#include<QLineEdit>
class cal_widget : public QGroupBox
{
Q_OBJECT
public:
explicit cal_widget(const QString& feature, QWidget *parent = nullptr);
QGridLayout* layout;
QPushButton* btn;
QLineEdit* ln1;
QLineEdit* ln2;
signals:
public slots:
};
#endif // CAL_WIDGET_H
cal_widget.cpp
#include "cal_widget.h"
cal_widget::cal_widget(const QString& feature, QWidget *parent) : QGroupBox(parent)
{
this->setTitle(feature);
layout = new QGridLayout();
btn= new QPushButton("OK");
ln1 = new QLineEdit("mm");
ln2 = new QLineEdit("pix");
layout->addWidget(ln1,0,0);
layout->addWidget(ln2,0,1);
layout->addWidget(btn,1,0,1,2);
this->setLayout(layout);
}

get value of an object in another class

How to get value of an object in another class? Such as I have 2 classes (class WidgetA and class WidgetB) both those classes are created by class Widget, like this:
//widget.h
#include <QWidget>
#include <QtGui>
class widgetA;
class widgetB;
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
private:
QComboBox* comboBox;
QStackedWidget* stackWidget;
widgetA *wa;
widgetB *wb;
};
class widgetA : public QWidget
{
Q_OBJECT
public:
widgetA(QWidget *parent = 0);
private:
QString mystr;
QLineEdit* lineEdit;
};
class widgetB : public QWidget
{
Q_OBJECT
public:
widgetB(QWidget *parent = 0);
QLabel* label;
};
And this is file widget.cpp:
#include "widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
comboBox = new QComboBox(this);
comboBox->addItem(tr("Widget A"));
comboBox->addItem(tr("Widget B"));
wa = new widgetA(this);
wb = new widgetB(this);
stackWidget = new QStackedWidget(this);
stackWidget->addWidget(wa);
stackWidget->addWidget(wb);
stackWidget->setCurrentIndex(0);
connect(comboBox, SIGNAL(currentIndexChanged(int)), stackWidget, SLOT(setCurrentIndex(int)));
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(comboBox);
layout->addWidget(stackWidget);
setLayout(layout);
}
widgetA::widgetA(QWidget *parent):
QWidget(parent)
{
lineEdit = new QLineEdit;
QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(lineEdit);
lineEdit->setReadOnly(true);
setLayout(lay);
}
widgetB::widgetB(QWidget *parent):
QWidget(parent)
{
label = new QLabel("Hello QT");
QHBoxLayout* lay = new QHBoxLayout;
lay->addWidget(label);
setLayout(lay);
}
How to I get text of QLabel label in WidgetB class into QString mystr in WidgetA class?
The cleanest way in terms of proper decoupling and separation of concerns is to add a getter forwarding label->text():
QString widgetB::text() const //or more specific name depending on the context
{
return label->text();
}
If you need notification when the value changes, add a signal:
class widgetB : public QWidget {
...
Q_SIGNALS:
void textChanged(const QString& text);
...
};
and emit it whenever you update the label.

qt connect cilds pushbutton

I have
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void getData();
private:
Ui::MainWindow *ui;
Dialog *second;
};
and
class Dialog: public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0); QDialog * dialog;
QPushButton *pushButton;
QPushButton *pushButton_2;
};
and I can connect pushbuttons in class Dialog with function getData() in class MainWindow (Dialog is a child of class Mainwindow)
I tried
connect(*second->pushButton, SIGNAL(clicked()), this,
SLOT(getData()));
but I got
error: no matching function for call to
‘MainWindow::connect(QPushButton&, const char [11], MainWindow* const, const char [11])’
How do i connect them?
if the dialog is child of QMainWindow subclass (as it would be) you should have something like:
MainWindow::MainWindow(...)
{
....
m_dialog = new Dialog(this);// in .h file it is defined as: "Dialog *m_dialog;"
....
connect(m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));
}
you don't have to write:
connect(*m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));
take a look at this qt code:
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
as you can see you must use pointer and not QObject.

Adding drop down menu to Qt GUI - error 'menubar' no declared

I'm trying to create a menu in Qt following this example http://doc.qt.nokia.com/latest/mainwindows-menus.html
but I keep getting the error 'menuBar' not declared in this scope
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
In context:
#include <QtGui>
#include "borderlayout.h"
#include "window.h"
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
//***Change this to whatever widget(s) the drawing area is. QPainter or something?
centralWidget->setPlainText(tr("DRAW HERE YAY"));
BorderLayout *layout = new BorderLayout;
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("File ..."), BorderLayout::North);
layout->addWidget(createLabel("Toolbar yo!"), BorderLayout::West);
//layout->addWidget(createLabel("Status bar"), BorderLayout::South);
//Maybe we could put in a status bar. For now let's not worry about it. It's not a requirement.
setLayout(layout);
createMenus();
setWindowTitle(tr("Border Layout"));
}
QLabel *Window::createLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label->setFrameStyle(QFrame::Box | QFrame::Raised);
return label;
}
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QLabel;
class QMenu;
class Window : public QWidget
{
Q_OBJECT
public:
Window();
private:
void createMenus();
QLabel *createLabel(const QString &text);
QMenu *saveMenu();
};
#endif
window.cpp
#include <QtGui>
#include "borderlayout.h"
#include "window.h"
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
//***Change this to whatever widget(s) the drawing area is. QPainter or something?
centralWidget->setPlainText(tr("DRAW HERE YAY"));
BorderLayout *layout = new BorderLayout;
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("File ..."), BorderLayout::North);
layout->addWidget(createLabel("Toolbar yo!"), BorderLayout::West);
//layout->addWidget(createLabel("Status bar"), BorderLayout::South);
//Maybe we could put in a status bar. For now let's not worry about it. It's not a requirement.
setLayout(layout);
createMenus();
setWindowTitle(tr("Border Layout"));
}
QLabel *Window::createLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label->setFrameStyle(QFrame::Box | QFrame::Raised);
return label;
}
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
The menu bar is a feature of the QMainWindow class.
Because your Window class is being inherited directly from QWidget, it does not have the menuBar method, hence your error.
You need to subclass your Window class from QMainWindow rather than QWidget.

Resources