Want to add pushbutton to setTimer and setText - c++

I am newbie in GUI design. Here, in sending and receiving messages (float, integer values) using DDS (OpenSplice) I am trying to add a pushButton to my already existing labels(displaying some float values as shown below), so that after a clicking on the pushButton, I should be able to see data in my label.
I tried adding a push button with the help of Qt Network sender Example. Now I get the error undefined reference to 'MainWindow::on_pushButton_clicked() in the file moc_mainwindow.cpp while building the project.
fastsender.cpp
FastSender::FastSender(QLabel *x, QObject *parent) : QObject(parent)
{
wSend = x;
dataTimer = new QTimer(this);
emergency = new QPushButton(tr("Emergency"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(emergency,QDialogButtonBox::ActionRole);
connect(emergency, SIGNAL(clicked()), this, SLOT(startsending()));
connect(dataTimer, SIGNAL(timeout()), this, SLOT(walk()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(buttonBox);
}
void FastSender::startsending()
{
emergency->setEnabled(false);
dataTimer->start(100); // Interval 0 means to refresh as fast as possible
}
int FastSender::walk()
{
msg->value= i+0.1;
snprintf (buf, MAX_MSG_LEN, "Message no. %d", i);
cout << "Writing message: \"" << msg->value << "\"" << endl;
status = talker->write(*msg, userHandle);
QString s= QString::number(msg->value, 'f',8);
wSend->setText(s);
}
fastsender.h
class FastSender : public QObject
{
Q_OBJECT
public:
explicit FastSender(QObject *parent = 0);
FastSender(QLabel *x, QObject *parent = 0);
~FastSender();
signals:
private:
QTimer* dataTimer;
QLabel *wSend;
DDS::DomainParticipantFactory_var dpf;
DDS::DomainParticipant_var parentDP;
DDS::Topic_var signalTopic;
DDS::DataReader_var parentReader;
DDS::DataWriter_var parentWriter;
fw::signalSeq_var msgSeq;
char * signalTypeName;
fw::signalDataWriter_var talker;
fw::signal *msg;
DDS::InstanceHandle_t userHandle;
DDS::Publisher_var fwPublisher;
int alpa;
QPushButton *emergency;
QDialogButtonBox *buttonBox;
public slots:
int walk();
int hello();
void startsending();
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fwdds = new FastWanDDS(ui->label);//receiving values are displayed in label
fwdds1 = new FastSender(ui->label_2);//Sending values are displayed in label_2
}
mainwindow.h
Ui::MainWindow *ui;
QTimer* timer;
int counter;
FastWanDDS *fwdds;
FastSender *fwdds1;
Any Help Appreciated.
Note: Only snippets of the code presented

I got it to work this way. Does this work for you?
mainwindow.cpp
#include "mainwindow.h"
#include "fastsender.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel* label = new QLabel("unchanged");
FastSender* fs = new FastSender(label);
setCentralWidget(fs);
}
MainWindow::~MainWindow()
{
}
fastsender.h
#ifndef FASTSENDER_H
#define FASTSENDER_H
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>
class FastSender : public QWidget
{
Q_OBJECT
public:
FastSender(QLabel* label, QWidget* parent = 0)
: QWidget(parent)
, _label(label)
, _timer(new QTimer)
, _emergency(new QPushButton(tr("Emergency")))
, _bbox(new QDialogButtonBox)
{
_bbox->addButton(_emergency, QDialogButtonBox::ActionRole);
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(_label);
layout->addWidget(_bbox);
setLayout(layout);
connect(_emergency, SIGNAL(clicked(bool)), this, SLOT(startsending()));
connect(_timer, SIGNAL(timeout()), this, SLOT(walk()));
}
public slots:
void startsending()
{
_emergency->setEnabled(false);
_timer->start(100);
}
int walk()
{
_label->setText("changed");
_emergency->setEnabled(true);
}
private:
QLabel* _label;
QTimer* _timer;
QPushButton* _emergency;
QDialogButtonBox* _bbox;
};
#endif // FASTSENDER_H

Related

Qt: Displaying an int in a message box

I have a timer/clock that displays a message X amounts of seconds (usually 10) after it has been clicked. I attempted to set up an int that one could set easily to change what X is, however I am having trouble with displaying the message, which includes the value of X, in a message box.
What I want:
Where the value "10" is changed by an int.
The specific line of code for the messagebox:
void MainWindow::messageBox(){
QMessageBox::information(this, "Warning!", "You clicked the button of doom " + timerLength/1000 + " seconds ago!");
} //timerLength is an int.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this); //Set up timer.
connect(timer, SIGNAL(timeout()), this, SLOT(showtime()));
//showtime();
timer->start();
messageBox();
otherTimer = new QTimer(this);
otherTimer->setSingleShot(true);
connect(otherTimer, SIGNAL(timeout()), this, SLOT(messageBox()));
}
void MainWindow::showtime(){ //Displays current time. Needs to be manually updated.
QTime time = QTime::currentTime();
QString lcdText = time.toString("mm:ss");
ui->lcdNumber->QLCDNumber::display(lcdText);
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::on_pushButton_clicked(){
int timerLength = 10000;
otherTimer->start(timerLength);
QPropertyAnimation *progresBar = new QPropertyAnimation(ui->graphicsView, "geometry");
progresBar->setDuration(timerLength);
QRect barLocation =ui->graphicsView->geometry();
progresBar->setStartValue(QRect(barLocation.x(), barLocation.y(), 0, barLocation.height()));
progresBar->setEndValue(barLocation);
progresBar->start();
}
void MainWindow::messageBox(){
QMessageBox::information(this, "Warning!", "You clicked the button of doom " + timerLength/1000 + " seconds ago!");
}
main.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
#include <QLCDNumber>
#include <QMessageBox>
#include <QPropertyAnimation>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
int timerLength;
public slots:
void showtime();
void messageBox();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QTimer *otherTimer;
};
#endif // MAINWINDOW_H
Please let me know if more information or explanation is required.
You can convert your number to QString like
void MainWindow::messageBox(){
QMessageBox::information(this, "Warning!", "You clicked the button of doom " + QString::number(timerLength/1000) + " seconds ago!");
} //timerLength is an int.
I tried it works in my app.

How to display QLabel inside qwidget from active QMdiAreaSubwindow in statusbar?

I have a app that uses QMdiArea.
I want the text in the statusbar to update when another QMdiAreaSubwindow becomes active.
So the text in the statusbar should become the same as the Qlabel text inside the QWidget which is been displayed inside the QMdiAreaSubwindow.
But i can't find a way to do this. Right now the statusbar only shows the text from latest created QMdiAreaSubwindow. But it won't update the text in the statusbar(With qlabel from the qwidget) when another QMdiAreaSubwindow is selected.
As you can see in the screenshot, the text in the statusbar keeps saying "test2", but I want it to change to "text" from the active QMdiAreaSubwindow.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <newwindow.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void NewSubWindow(QString name);
void createStatusBar(QString name);
private slots:
void on_actionNew_triggered();
void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);
private:
Ui::MainWindow *ui;
NewWindow *nDialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mdisubwidget.h"
#include "newwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
nDialog = new NewWindow();
connect(nDialog,&NewWindow::transmit,this,&MainWindow::NewSubWindow);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::NewSubWindow(QString name) {
// new Widget to add to mdiareasubwindow
mdisubwidget *mdiwidget = new mdisubwidget();
mdiwidget->addName(name);
mdiwidget->setWindowTitle(name);
// Create new mdiAreaSubWindow
ui->mdiArea->addSubWindow(mdiwidget);
// Show mdiArea
mdiwidget->show();
}
void MainWindow::on_actionNew_triggered()
{
nDialog->show();
}
void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
mdisubwidget *mdiwidget = new mdisubwidget(arg1->widget());
qDebug() << "name" << mdiwidget->returnName();
createStatusBar(mdiwidget->returnName());
}
void MainWindow::createStatusBar(QString name)
{
statusBar()->showMessage("chart = "+name);
}
mdisubwidget.h
#ifndef MDISUBWIDGET_H
#define MDISUBWIDGET_H
#include <QWidget>
namespace Ui {
class mdisubwidget;
}
class mdisubwidget : public QWidget
{
Q_OBJECT
public:
explicit mdisubwidget(QWidget *parent = nullptr);
void addName(QString name);
QString returnName();
~mdisubwidget();
private:
Ui::mdisubwidget *ui;
};
#endif // MDISUBWIDGET_H
mdisubwidget.cpp
#include "mdisubwidget.h"
#include "ui_mdisubwidget.h"
#include "mainwindow.h"
QString currentName;
mdisubwidget::mdisubwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::mdisubwidget)
{
ui->setupUi(this);
}
void mdisubwidget::addName(QString name) {
ui->label_2->setText(name);
currentName = name;
}
QString mdisubwidget::returnName() {
return currentName;
}
mdisubwidget::~mdisubwidget()
{
delete ui;
}
NewWindow.h:
#ifndef NEWWINDOW_H
#define NEWWINDOW_H
#include <QWidget>
namespace Ui {
class NewWindow;
}
class NewWindow : public QWidget
{
Q_OBJECT
public:
explicit NewWindow(QWidget *parent = nullptr);
~NewWindow();
signals:
void transmit(QString name);
private slots:
void on_pushButton_clicked();
private:
Ui::NewWindow *ui;
};
#endif // NEWWINDOW_H
NewWindow.cpp:
#include "newwindow.h"
#include "ui_newwindow.h"
#include "mainwindow.h"
NewWindow::NewWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::NewWindow)
{
ui->setupUi(this);
}
NewWindow::~NewWindow()
{
delete ui;
}
void NewWindow::on_pushButton_clicked()
{
QString name = ui->lineEdit->text();
emit transmit(name);
}
ok you're using Qt Designer to connect the signal of subWindowActivated to the slot of on_mdiArea_subWindowActivated of your MainWindow, double check with qDebug in your on_mdiArea_subWindowActivated function if the name of your selected sub window appears on the console as you tried to change your current mdi sub window so follow my code snippets to find your way:
connect(ui->mdiArea, &QMdiArea::subWindowActivated, this, &DesignerWindow::activeViewChanged);
activeViewChanged():
void DesignerWindow::activeViewChanged(QMdiSubWindow *activeSubWindow)
{
// checks if there is no active sub window defined or the number of subwindows
// are zero then return
if (!activeSubWindow)
return;
if (ui->mdiArea->subWindowList().count() == 0) {
ui->itemsTree->clear();
return;
}
// defines the current Midi, View and graphical Scene when current sub window changes
currentMidi = reinterpret_cast<MidiWindow*>(activeSubWindow->widget());
currentView = reinterpret_cast<HMIView*>(currentMidi->internalView());
currentScene = reinterpret_cast<HMIScene*>(currentMidi->internalScene());
ItemsToolBar::ItemType currentType = currentScene->itemType();
itemsToolBar->selectItemType(currentType);
// updates the widgets and labels in status bar related to current midi sub window
updateScale(currentView->zoomFactor() * 100);
updateSelected();
updateItemsTree();
updateRendererType();
}
for example for updating the label in the status bar that holds the zooming factor of the current mdiSubWindow I wrote the updateScale procedure as below:
void DesignerWindow::updateScale(double _scale)
{
scale = static_cast<int>(_scale);
scaleLbl->setText(QString("%1%").arg(scale));
}
and finally I've noticed that your creating a label in status bar every time that you try to update the text in it, please avoid such a procedure and create a QLabel object and add it to your status bar as a permanent widget like below:
scaleLbl = new QLabel(this);
scaleLbl->setFrameStyle(QFrame::Sunken | QFrame::Panel);
scaleLbl->setMinimumWidth(50);
statusBar()->addPermanentWidget(scaleLbl);

Using QPainter to draw a line between QWidgets

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.

Program runs but nothing displays Qt c++

Hi my program runs with no errors but nothing is displayed on screen, a window is meant to pop up which runs another program with a QProcess, this program runs fine. code follows:
header file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtGui>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void setTimer();
void displayAdvice();
void cancelTimer();
void addAdvice();
void processDone(int);
private:
QLabel* timerLbl;
QLineEdit* timerEdt;
QTextEdit* adviceEdt;
QPushButton* okBtn;
QPushButton* cancelBtn;
QTimer* timer;
QProcess *process;
void setupGui();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setupGui();
}
Widget::~Widget()
{
delete ui;
}
void Widget::setupGui()
{
timerLbl = new QLabel("Timer set interval in seconds");
timerEdt = new QLineEdit();
adviceEdt = new QTextEdit();
this->adviceEdt->setReadOnly(true);
okBtn = new QPushButton("OK");
cancelBtn = new QPushButton("Cancel");
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(timerLbl);
layout->addWidget(timerEdt);
layout->addWidget(okBtn);
layout->addWidget(adviceEdt);
layout->addWidget(cancelBtn);
this->setWindowTitle("Advice");
this->setLayout(layout);
connect(okBtn,SIGNAL(clicked()), this, SLOT(setTimer()));
connect(cancelBtn, SIGNAL(clicked()), this, SLOT(cancelTimer()));
connect(timer, SIGNAL(timeout()),this, SLOT(displayAdvice()));
}
void Widget::setTimer()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this, SLOT(cancelTimer()));
QString setting = this->timerEdt->text();
bool ok;
int set = setting.toInt(&ok,10);
set = set * 1000;
timer->setInterval(set);
timer->start();
timerEdt->setReadOnly(true);
okBtn->setDown(true);
}
void Widget::cancelTimer()
{
timer->stop();
adviceEdt->clear();
okBtn->setDown(false);
timerEdt->clear();
timerEdt->setReadOnly(false);
}
void Widget::displayAdvice()
{
process = new QProcess(this);
process->start("C:/Users/Dmon/Desktop/47039949 COS3711 Assignment 2/Question 4/Ass2Q4Part1-build-desktop/debug/Ass2Q4Part1.exe");
connect(process, SIGNAL(readyReadStandardOutput()),this, SLOT(addAdvice()));
connect(process, SIGNAL(finished(int)),this, SLOT(processDone(int)));
}
void Widget::addAdvice()
{
QByteArray data = process->readAllStandardOutput();
QString strToWrite = data;
this->adviceEdt->clear();
this->adviceEdt->append(strToWrite);
}
void Widget::processDone(int)
{
process->close();
process->deleteLater();
process=0;
}
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Program runs with nothing displayed then eventually exits with no errors after about 15 seconds.
Your problem is that you're already using a user interface coming from the form file (.ui file). You need to decide which one you wish to use.
To fix your code, all you need to do is remove all references to the Ui namespace. Simply remove the below:
namespace Ui {
class Widget;
}
//
private:
Ui::Widget *ui;
//
#include "ui_widget.h"
//
delete ui;
Also, note that this line is working with a null or undefined pointer value - you never create the timer instance:
connect(timer, SIGNAL(timeout()),this, SLOT(displayAdvice()));
Finally, there's no reason whatsoever to allocate the member widgets on the heap. It does, in fact, waste a bit of heap memory since QWidget instances are very small.
Here's how your code could look. I've put it all in a single file, to keep it short. You obviously don't need it in a single file. I've also made the UI a bit more compliant with usual expectations. E.g. controls that can't be interacted with should be disabled.
// main.cpp
#include <QApplication>
#include <QtWidgets>
#include <QTimer>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
private slots:
void setTimer();
void displayAdvice();
void cancelTimer();
void addAdvice();
void processDone(int);
private:
QLabel m_timerLbl;
QLineEdit m_timerEdt;
QTextEdit m_adviceEdt;
QPushButton m_okBtn;
QPushButton m_cancelBtn;
QTimer m_timer;
QProcess m_process;
void setupGui();
};
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
setupGui();
}
void Widget::setupGui()
{
m_timerLbl.setText("Timer set interval in seconds");
m_adviceEdt.setReadOnly(true);
m_okBtn.setText("OK");
m_cancelBtn.setText("Cancel");
m_cancelBtn.setEnabled(false);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(&m_timerLbl);
layout->addWidget(&m_timerEdt);
layout->addWidget(&m_okBtn);
layout->addWidget(&m_adviceEdt);
layout->addWidget(&m_cancelBtn);
setWindowTitle("Advice");
connect(&m_okBtn, SIGNAL(clicked()), SLOT(setTimer()));
connect(&m_cancelBtn, SIGNAL(clicked()), SLOT(cancelTimer()));
connect(&m_timer, SIGNAL(timeout()), SLOT(displayAdvice()));
connect(&m_timer, SIGNAL(timeout()), SLOT(cancelTimer()));
connect(&m_process, SIGNAL(readyReadStandardOutput()), SLOT(addAdvice()));
connect(&m_process, SIGNAL(finished(int)), SLOT(processDone(int)));
}
void Widget::setTimer()
{
QString const setting = m_timerEdt.text();
bool ok;
int set = setting.toInt(&ok,10) * 1000;
m_timer.setInterval(set);
m_timer.start();
m_timerEdt.setEnabled(false);
m_okBtn.setEnabled(false);
m_cancelBtn.setEnabled(true);
}
void Widget::cancelTimer()
{
m_timer.stop();
m_adviceEdt.clear();
m_timerEdt.clear();
m_okBtn.setEnabled(true);
m_timerEdt.setEnabled(true);
m_cancelBtn.setEnabled(false);
}
void Widget::displayAdvice()
{
m_process.start("bash", QStringList() << "-c" << "echo 'Hello!'");
#if 0
m_process.start(QDir::homePath() +
"/Desktop/47039949 COS3711 Assignment 2/Question 4/Ass2Q4Part1-build-desktop/debug/Ass2Q4Part1.exe");
#endif
}
void Widget::addAdvice()
{
QByteArray const data = m_process.readAllStandardOutput();
m_adviceEdt.setPlainText(QString::fromLocal8Bit(data));
}
void Widget::processDone(int)
{
m_process.close();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"

Qt signal-slot not working

I want to update text in a label in a first window from a second window where is a line edit to write some text. This text should be dispaly in first window.
I spend a week for it.
A famous connect doesn't work.
Is somebody who correct below code and explain how connect should work?
I use Qt in version 5.1.1
firstwindow.h
#ifndef FIRSTWINDOW_H
#define FIRSTWINDOW_H
#include <QMainWindow>
#include "secondwindow.h"
namespace Ui {
class Firstwindow;
}
class Firstwindow : public QMainWindow
{
Q_OBJECT
public:
explicit Firstwindow(QWidget *parent = 0);
~Firstwindow();
public slots:
void addEntry();
private slots:
void on_pushButton_clicked();
private:
Ui::Firstwindow *ui;
Secondwindow *asecondwindow;
Secondwindow *absecondwindow;
Secondwindow *abcsecondwindow;
};
#endif // FIRSTWINDOW_H
secondwindow.h
#ifndef SECONDWINDOW_H
#define SECONDWINDOW_H
#include <QDialog>
#include <QtWidgets>
namespace Ui {
class Secondwindow;
}
class Secondwindow : public QDialog
{
Q_OBJECT
public:
explicit Secondwindow(QWidget *parent = 0);
~Secondwindow();
QLineEdit *lineEdit;
private slots:
void on_pushButton_clicked();
private:
Ui::Secondwindow *ui;
QPushButton *pushButton;
};
#endif // SECONDWINDOW_H
main.cpp
#include "firstwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Firstwindow w;
w.show();
return a.exec();
}
firstwindow.cpp
#include "firstwindow.h"
#include "ui_firstwindow.h"
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
Firstwindow::Firstwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Firstwindow)
{
ui->setupUi(this);
asecondwindow = new Secondwindow();
QObject::connect(asecondwindow->lineEdit,SIGNAL(textChanged()),this, SLOT(addEntry()));
}
Firstwindow::~Firstwindow()
{
delete ui;
delete asecondwindow;
delete absecondwindow;
delete abcsecondwindow;
}
void Firstwindow::on_pushButton_clicked()
{
absecondwindow = new Secondwindow;
absecondwindow->exec();
}
void Firstwindow::addEntry()
{
abcsecondwindow = new Secondwindow;
if (abcsecondwindow->exec()) {
QString name = abcsecondwindow->lineEdit->text();
ui->label->setText(name);
}
}
secondwindow.cpp
#include "secondwindow.h"
#include "ui_secondwindow.h"
#include <QDialog>
Secondwindow::Secondwindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::Secondwindow)
{
ui->setupUi(this);
}
Secondwindow::~Secondwindow()
{
delete ui;
}
void Secondwindow::on_pushButton_clicked()
{
// emit ui->lineEdit->textChanged();
QDialog::accept();
}
I see the following issues:
QLineEdit does not have a signal textChanged(). It should be textChanged(const QString &) instead. So you have to install your connection like:
QObject::connect(asecondwindow->lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(addEntry(const QString &)));
Please note that I changed the Firstwindow::addEntry() slot to Firstwindow::addEntry(const QString &) to match the signal's signature.
I cannot find when and where your QLineEdit member variable of the Secondwindow class is created.
There is a fundamental design problem with what you're doing. There's no need to expose the second window's internal properties to the first window. Just listen for changes within the second window and emit a signal whenever it changes. Then the first window can just listen to the changes on the second window.
Here's a full example showing what I mean. main.cpp:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
class SecondWindow : public QDialog {
Q_OBJECT
public:
SecondWindow(QMainWindow *parent = 0) : QDialog(parent) {
QLineEdit *edit = new QLineEdit;
QPushButton *close = new QPushButton(QStringLiteral("close"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(edit);
layout->addWidget(close);
setLayout(layout);
connect(edit, SIGNAL(textChanged(QString)), this, SIGNAL(textChanged(QString)));
connect(close, SIGNAL(clicked()), this, SLOT(close()));
}
signals:
void textChanged(const QString &text);
};
class FirstWindow : public QMainWindow {
Q_OBJECT
public:
FirstWindow(QMainWindow *parent = 0) : QMainWindow(parent) {
QWidget *central = new QWidget(this);
QPushButton *button = new QPushButton(QStringLiteral("Open"));
label = new QLabel(QStringLiteral("Output appears here"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button);
layout->addWidget(label);
central->setLayout(layout);
setCentralWidget(central);
connect(button, SIGNAL(clicked()), this, SLOT(createWindow()));
}
private slots:
void createWindow() {
SecondWindow *window = new SecondWindow(this);
connect(window, SIGNAL(textChanged(QString)), this, SLOT(setLabelText(QString)));
window->resize(300, 300);
window->exec();
}
void setLabelText(const QString &text) {
label->setText(text);
}
private:
QLabel *label;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FirstWindow w;
w.resize(400, 400);
w.show();
return a.exec();
}
#include "main.moc"
Not that the SecondWindow listens for changes on the QLineEdit and emits its own signal when that value changes. Then the FirstWindow just connects to that signal and changes its own QLabel whenever it receives the signal.