uninitialized reference member 'Widget::handler' - c++

This error;
C:\Users\Seb\Desktop\SDIcw2\widget.cpp:6: error: uninitialized reference member 'Widget::handler' [-fpermissive]
Widget::Widget(QWidget *parent) :
^
has occurs after I added an event handler for my combination box. Does anyone know why? The widget ran fine before so im not sure as to why!
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QString>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::getList(SDI::shipHandler& shipHandler)
{
handler = shipHandler;
}
void Widget::populateCombo()
{
ui->comboBox->addItem("Select Vessel...");
for(std::vector<SDI::navalVessels*>::const_iterator i = handler.ships.begin(); i != handler.ships.end(); ++i)
{
SDI::navalVessels* ship = *i;
QString qstr = QString::fromStdString(ship->name);
ui->comboBox->addItem(qstr);
}
}
void Widget::on_comboBox_currentIndexChanged(int index)
{
for(std::vector<SDI::navalVessels*>::const_iterator i = handler.ships.begin(); i != handler.ships.end(); ++i)
{
SDI::navalVessels* ship = *i;
QString qstr = QString::fromStdString(ship->name);
QString str = ui->comboBox->currentText();
if (str == qstr)
{
//do something here
}
}
}
trying to change the values of boxes depending on whats selected in the combo box.

You haven't shown widget.h, but presumably you have a private section that looks something like:
private:
Ui::Widget *ui;
ShipHandler *handler;
Your compiler is reminding you that uninitialized pointers can be dangerous, so you need to make sure you change your constructor to initialize them:
Widget::Widget(QWidget *parent)
: QWidget(parent),
ui(new Ui::Widget),
handler(0)
{
ui->setupUi(this);
}

Related

Qt passing a Value from one window to antother

I am quite new to programming, so hopefully someone can help me out. After calculating a value with a function and storing it into the variable with a setter, I want to call it in my viswindow.cpp to visualize it in a Graph. In the mainwindow the right value is calculated and stored by pressing a button, which also opens the viswindow. I tried to pass it with a pointer or with signals and slots, but it wouldn't work.
mainwindow.cpp:
double MainWindow::berechneFussabdruck(double strecke, int anzahl, double sfcWert)
{
if(strecke <= 1500 && strecke > 0){
double aequivalente_Co2 = (((sfcWert*3.116*50*(((0.0011235955)*(strecke-2*48.42))+0.233))*0.001)/anzahl);
setAequivalente(aequivalente_Co2);
}
else{
double aequivalente_Co2 = (((sfcWert*3.116*75*(((0.0011235955)*(strecke-2*208))+0.833))*0.001)/anzahl);
setAequivalente(aequivalente_Co2);
}
return aequivalente_Co2;
}
Button to open viswindow:
void MainWindow::on_pushButton_clicked()
{
MainWindow::berechneFussabdruck(strecke, anzahl, sfcWert);
visWindow = new VisWindow(this);
visWindow->show();
}
viswindow.cpp:
VisWindow::VisWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::VisWindow)
{
ui->setupUi(this);
...
*set0 << aequivalente_Co2 << aequivalente_Co2;
*set1 << 11.07 << 0;
*set2 << 0 << 0.49;
...
}
Thanks for helping!
Here are examples of:
Single time value transfer from MainWindow to VisWindow in constructor parameter.
Value update from MainWindow to VisWindow by slot-signal chain.
Value update from MainWindow to VisWindow by direct call of VisWindow->updateValue() slot as regular function.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "viswindow.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
signals:
void updatevalue(const double value);
private:
Ui::MainWindow *ui;
VisWindow * m_viswindow;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::on_pushButton_clicked() {
m_viswindow = new VisWindow(12.34, nullptr);
connect(this, &MainWindow::updatevalue,
m_viswindow, &VisWindow::updateValue);
m_viswindow->show();
}
void MainWindow::on_pushButton_2_clicked() {
emit updatevalue(23.45);
}
void MainWindow::on_pushButton_3_clicked() {
m_viswindow->updateValue(45.67);
}
viswindow.h
#ifndef VISWINDOW_H
#define VISWINDOW_H
#include <QWidget>
namespace Ui {
class VisWindow;
}
class VisWindow : public QWidget
{
Q_OBJECT
public:
explicit VisWindow(const double value, QWidget *parent = nullptr);
~VisWindow();
public slots:
void updateValue(const double value);
private:
Ui::VisWindow *ui;
};
#endif // VISWINDOW_H
viswindow.cpp
#include "viswindow.h"
#include "ui_viswindow.h"
VisWindow::VisWindow(const double value, QWidget *parent) :
QWidget(parent),
ui(new Ui::VisWindow)
{
ui->setupUi(this);
ui->lblValue->setText(QString::number(value,'f',2));
this->setWindowTitle("VisWindow");
}
VisWindow::~VisWindow() {
delete ui;
}
void VisWindow::updateValue(const double value) {
ui->lblValue->setText(QString::number(value,'f',2));
}

Qt - Add item from a second window to ListWidget in MainWindow

I have a QDialog where I can enter a Text and add it to a ListWidget inside the MainWindow after I clicked OK. Therefore I created a Method in the MainWindow class AddMessageToList(message) where the entered message is added to the ListWidget. I call the Method inside my ListAdder class (2nd Window). However nothing gets added to the ListWidget.
Here are the codes:
listadder.cpp
#include "listadder.h"
#include "ui_listadder.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
ListAdder::ListAdder(QWidget *parent) :
QDialog(parent),
ui(new Ui::ListAdder)
{
ui->setupUi(this);
}
ListAdder::~ListAdder()
{
delete ui;
}
void ListAdder::on_buttonBox_accepted()
{
MainWindow mw;
QString message = ui->lnText->text();
mw.AddMessageToList(message);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "listadder.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbt_Edit_clicked()
{
ListAdder lsadd;
lsadd.exec();
}
void MainWindow::AddMessageToList(QString message)
{
ui->lsItems->addItem(message);
}
Fixed it!
I created a QString method GetMessage() where I return the entered text. In the MainWindow I call the method and add the text to the ListWidget.
listadder.cpp:
#include "listadder.h"
#include "ui_listadder.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
ListAdder::ListAdder(QWidget *parent) :
QDialog(parent),
ui(new Ui::ListAdder)
{
ui->setupUi(this);
}
ListAdder::~ListAdder()
{
delete ui;
}
QString ListAdder::GetMessage()
{
QString message = ui->lnText->text();
return message;
}
void ListAdder::on_buttonBox_accepted()
{
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "listadder.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbt_Edit_clicked()
{
ListAdder lsadd;
lsadd.exec();
ui->lsItems->addItem(lsadd.GetMessage());
}

QLabel show images like a video

I try to let a QLabel show images like a video.
I want this display my images from f0000.png to f0039.png slowly so I can see progress.
For some reason my for loop starts with 50.
When I see the program run it only show one image or it change too fast I can't see the progress.
How to fix it let it show images like video.
you can use a Qtimer and set the speed as faster as you need
header:
#include <QMainWindow>
#include <QTimer>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
public slots:
void updateLabel();
private:
Ui::MainWindow *ui;
QTimer* _timer;
int index{0};
QString pixResource{};
};
and impl.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
_timer = new QTimer(parent);
connect(_timer, SIGNAL(timeout()), this, SLOT(updateLabel()));
_timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
_timer->stop();
delete _timer;
}
void MainWindow::updateLabel()
{
if (index >= 10)
{
index = 0;
}
qDebug() << "index: " << index;
pixResource = "res/foo/image/" + QString::number(index) + ".png";
qDebug() << "now the res: " << pixResource;
index++;
}

Global variable contains garbage when push button is clicked

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include <iostream>
char * ip_ch;
void test(const char* a)
{
qDebug()<<"test is "<<a;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_lineEdit_editingFinished()
{
QString st_ip_ch=ui->lineEdit->text();
QByteArray ip_ch_temp = st_ip_ch.toLatin1();
ip_ch = ip_ch_temp.data();
qDebug()<<"ip_ch is "<<ip_ch;
test(ip_ch);
}
void MainWindow::on_pushButton_clicked()
{
qDebug()<<"when push button ";
test(ip_ch);
}
The code uses the following UI. I want to type some string in the line input widget like this, and then push the button.
It prints out this:
ip_ch is 123
test is 123
When button is clicked, it prints:
test is `??`
Why does ip_ch point to garbage?
ip_ch becomes a dangling pointer as soon as on_lineEdit_editingFinished() returns. That's because you're making it point to a temporary buffer ip_ch_temp that goes out of scope:
ip_ch = ip_ch_temp.data(); // Don't do that!
Since you're coding in C++, you shouldn't be writing C. Store your Latin1 representation by value:
class MainWindow : public QMainWindow {
QByteArray ip_ch;
Ui::MainWindow ui;
public:
MainWindow(QWidget * parent = 0) {
ui.setupUi(this);
}
// no explicit destructor needed!
void test(const QByteArray & data) { qDebug() << "test:" << data; }
void on_lineEdit_editingFinished()
{
ip_ch = ui.lineEdit->test().toLatin1();
qDebug() << "ip_ch is" << ip_ch;
test(ip_ch);
}
void on_pushButton_clicked()
{
qDebug() << "button was pushed";
test(ip_ch);
}
};
Other problems with your code:
#include <QDebug>, not <QtDebug>
#include <QMainWindow>, not <qmainwindow.h>
Most likely, you don't need to derive your dialog from QMainWindow; derive from a QDialog instead.
Don't use a pointer to ui; store it by value instead. That way you have less code that can go wrong: let the compiler manage memory for you. That's what it's for!

missing derived class with QObject

I am getting the error ~mainwindow.cpp:9: error: redefinition of 'MainWindow::MainWindow(QWidget*)' MainWindow::MainWindow(QWidget *parent) : and I am pretty sure it is complaining about the wrong derived class name. The base class is MainWindow but I do not see what the derived class would be.
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void coreEng::requestPage(){
QUrl url("http://www.nyctelecomm.com");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void coreEng::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}
else
{
}
// delete reply();
}
void MainWindow::on_lcdNumber_overflow()
{
QByteArray replyData = netReply->readAll();
}
void MainWindow::on_pushButton_clicked()
{
QObject::on_pushButton_clicked(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QObject>
#include <QNetworkAccessManager>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0):
QMainWindow(parent)
{
nam = new QNetworkAccessManager();
}
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
//void connect();
void on_pushButton_clicked();
void requestPage();
void finishedSlot(QNetworkReply* reply);
private slots:
void on_lcdNumber_overflow();
private:
QNetworkAccessManager* nam;
};
#endif // MAINWINDOW_H
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow mConnect;
mConnect.on_pushButton_clicked();
mConnect.requestPage();
MainWindow w;
w.show();
return a.exec();
}
You have defined MainWindow::MainWindow two times. First in header file and second in source file. That is said in error message.
explicit MainWindow(QWidget *parent = 0):
QMainWindow(parent)
{
nam = new QNetworkAccessManager();
}
//
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}