Global variable contains garbage when push button is clicked - c++

#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!

Related

Send parameters with dynamically generated QComboBox

I want to insert a QComboBox inside a QTableWidget. When I change the Index of the CB I'll call a methode to change the status in the sqlite table. But for this, I need do pass two parameters to the methode. The ID(First element of the row), and the current index of the CB.
I generate the QTableWidget like that:
...
for(int i = 0; i < dbModel->rowCount();i++){
row = dbModel->record(i);
ui->tW_Services->setItem(i,0,new QTableWidgetItem(row.field(0).value().toString()));
ui->tW_Services->setItem(i,1,new QTableWidgetItem(row.field(1).value().toString()));
ui->tW_Services->setItem(i,2,new QTableWidgetItem(row.field(2).value().toString()));
ui->tW_Services->setItem(i,3,new QTableWidgetItem(row.field(3).value().toString()));
ui->tW_Services->setItem(i,4,new QTableWidgetItem(row.field(4).value().toString() + "€"));
ui->tW_Services->setItem(i,5,new QTableWidgetItem(row.field(5).value().toString() + "€"));
//ui->tW_Services->setItem(i,6,new QTableWidgetItem(row.field(6).value().toString()));
QComboBox* combo = new QComboBox();
combo->addItem("open");
combo->addItem("paid");
if(row.field(6).value().toString() != "paid") combo->setCurrentIndex(0);
else combo->setCurrentIndex(1);
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboChanged(int)));
ui->tW_Services->setCellWidget(i,6,combo);
}
...
and the slot looks like that:
void MainWindow::onComboChanged(int ID)
{
qDebug() << "ID:" << QString::number(ID);
}
But when I try to create the method with 2 args:
void MainWindow::onComboChanged(int ID,int index)
{
qDebug() << "ID:" << QString::number(ID);
}
and attach a second parameter and calling the slot like:
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboChanged(row.field(0).value().toInt(),int)));
I get:
qt.core.qobject.connect: QObject::connect: No such slot MainWindow::onComboChanged(row.field(0).value().toInt(),int)
A minimal reproducible example:
There's just a single pushbutton and a qtablewidget
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
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 onComboChanged(int ID, int index);
void onComboChanged2(int index);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QComboBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setRowCount(3);
for(int i = 0; i < 3;i++){
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem("Test"));
ui->tableWidget->setItem(i,2,new QTableWidgetItem("Test 2"));
QComboBox* combo = new QComboBox();
combo->addItem("open");
combo->addItem("paid");
combo->setCurrentIndex(0);
int j = i+1;
//NOT WORKING
//connect(combo, SIGNAL(currentIndexChanged(int)),this,
// [=](int index) {this->onComboChanged(j, index);});
//WORKING
//connect(combo, SIGNAL(currentIndexChanged(int)),SLOT(onComboChanged2(int)));
ui->tableWidget->setCellWidget(i,3,combo);
}
}
void MainWindow::onComboChanged(int ID, int index)
{
qDebug() << "ID: " << ID << "index: "<< index;
}
void MainWindow::onComboChanged2(int index)
{
qDebug() << "ID: " << index;
}
Since currentIndexChanged only has one parameter, your slot cannot capture more than that. But, since the row ID does not change on emission, you can wrap onComboChanged into a lambda as shown here, which captures the row by copy:
connect(combo, &QComboBox::currentIndexChanged,
this, [=](int index) {this->onComboChanged(i, index);});
Since your code is not a minimal reproducible example, I could not test this. Let me know, if it doesn't work.

Qt QThread stop when I need

//Work.h
#ifndef WORK_H
#define WORK_H
#include <QDebug>
#include <QObject>
#include <QThread>
class Work : public QObject {
Q_OBJECT
public:
explicit Work(QObject *parent = nullptr);
public slots:
void snap();
void setStatus();
signals:
private:
bool status;
};
#endif // WORK_H
//Work.cpp
#include "Work.h"
Work::Work(QObject *parent) : QObject(parent) { status = true; }
void Work::snap() {
status = true;
while (true) {
if (status) {
qDebug() << "Work thread: " << QThread::currentThreadId();
} else {
qDebug() << "STOP";
break;
}
}
}
void Work::setStatus() { status = false; }
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
#include "Work.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
Work *work;
QThread thread;
};
#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);
work = new Work();
work->moveToThread(&thread);
thread.start();
connect(ui->startButton, SIGNAL(clicked()), work, SLOT(snap()));
connect(ui->stopButton, SIGNAL(clicked()), work, SLOT(setStatus()));
}
MainWindow::~MainWindow() {
thread.terminate();
delete ui;
}
//main.cpp
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
qDebug() << QThread::currentThreadId();
w.show();
return a.exec();
}
I use MainWindow to display, Work to do something. And I use work->moveToThread(&thread).
Click start button to execute snap function in Work, what I want to do is when I click stop button, the snap function output STOP. And I can still start and stop whenever I like.
But I fail. It seems impossible to change the status during the while loop. Work doesn't get the stopButton clicked signal. Is it because of priority?
Could anyone give me some advices?
Firstly consider your Work::snap implementation...
void Work::snap() {
status = true;
while (true) {
if (status) {
qDebug() << "Work thread: " << QThread::currentThreadId();
} else {
qDebug() << "STOP";
break;
}
}
}
Once started it never yields control to the Qt event loop. Now consider the connect call...
connect(ui->stopButton, SIGNAL(clicked()), work, SLOT(setStatus()));
Since ui->stopButton and work have different thread affinities this is effectively a queued connection and requires the receiver to have an active event loop. Hence the call to setStatus will remain pending forever.
A better way to achieve your goal might be to make use of a simple atomic bool...
std::atomic<bool> status;
and change the connect call to modify status directly using a lambda (untested)...
connect(ui->stopButton, &QPushButton::clicked, [this]{ work->setStatus(); });
I solve it.
I add a slot and a signal in MainWindow and change stop slot.
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::start);
connect(this, &MainWindow::startSnap, work, &Work::snap);
// start slot
void MainWindow::start() {
thread.start();
emit startSnap();
}
void MainWindow::stop() {
if (thread.isRunning()) {
thread.requestInterruption();
}
}
And change the codes in Work::snap
void Work::snap() {
while (true) {
if (QThread::currentThread()->isInterruptionRequested()) {
qDebug() << "STOP";
QThread::currentThread()->exit();
return;
} else {
qDebug() << "Work thread: " << QThread::currentThreadId();
}
}
}
The key codes are:
thread.requestInterruption();(MainWindow::stop)
QThread::currentThread()->exit();(Work::snap)
thread.start();(MainWindow::start)

QTextEdit promoted to Construct a Console Window

I try to construct a promoted QTextEdit Console widget for my Gui in which i redirect all std::cout. For the redirection i mostly followed this tutorial:
My code looks like this:
myConsoleStream.cpp:
#include "myconsolestream.h"
myConsoleStream::myConsoleStream(std::ostream &stream, QTextEdit* text_edit,QWidget *parent)
:std::basic_streambuf<char>()
,m_stream(stream)
,myConsole(parent)
{
this->log_window = text_edit;
this->m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
myConsoleStream::~myConsoleStream()
{
this->m_stream.rdbuf(this->m_old_buf);
}
void myConsoleStream::registerMyConsoleMessageHandler()
{
qInstallMessageHandler(myConsoleMessageHandler);
}
void myConsoleStream::myConsoleMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg)
{
std::cout << msg.toStdString().c_str();
}
std::streamsize myConsoleStream::xsputn(const char *p, std::streamsize n)
{
QString str(p);
if(str.contains("\n"))
{
QStringList strSplitted = str.split("\n");
this->log_window->moveCursor (QTextCursor::End);
this->log_window->insertPlainText (strSplitted.at(0)); //Index 0 immer noch auf der alten Zeile
for(int i = 1; i < strSplitted.size(); i++)
{
this->log_window->append(strSplitted.at(i));
}
}
else
{
log_window->moveCursor (QTextCursor::End);
log_window->insertPlainText (str);
}
return n;
}
myConsoleStream.h:
#ifndef Q_DEBUGSTREAM_H
#define Q_DEBUGSTREAM_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QObject>
#include <QWidget>
#include "QTextEdit"
#include <QApplication>
#include "myconsole.h"
class myConsoleStream : public std::basic_streambuf<char>, myConsole
{
public:
myConsoleStream(std::ostream &stream, QTextEdit* text_edit, QWidget *parent);
virtual ~myConsoleStream();
static void registerMyConsoleMessageHandler();
private:
static void myConsoleMessageHandler(QtMsgType, const QMessageLogContext &, const QString &msg);
protected:
// Diese Funktion wird aufgerufen wenn std::endl im Stream erscheint
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
log_window->append("");
}
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n);
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
QTextEdit* log_window;
};
#endif // Q_DEBUGSTREAM_H
I tried to make this class a subclass of an additional class to which I promoted the QTextEdit to.Which looks like this:
#include "myconsole.h"
myConsole::myConsole(QWidget *parent)
:QTextEdit(parent)
{
}
myConsole::~myConsole()
{
}
Last but not least my Mainwindow Class looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBoxLayout>
#include <QTimer>
#include "myconsolestream.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
new myConsoleStream(std::cout, this->ui->Console,this);//Redirect Console output to QTextEdit
myConsoleStream::registerMyConsoleMessageHandler(); //Redirect qDebug() output to QTextEdit
std::cout << "Hallo" << std::endl;
}
MainWindow::~MainWindow()
{
delete ui;
}
If I execute this there appears a second QTextEdit in the left upperhand corner of my Mainwindow, which I dont want to have there. And I cant figure out why this is appearing or where this is created?
Btw I am using qt5.5 and QtCreator for that matter.
It looks like you create your own QTextEdit with the myConsole class and you also redirect your output to this->ui->Console which I guess is created with the UI designer and also an instance of QTextEdit. myConsole in then displayed in the top left of your window because you don't provide it with any layout information and just a parent widget.
To clarifiy, your myConsoleStream is derived from myConsole which in term is derived from QTextEdit, so your myConsoleStream is also an object of type QTextEdit itself.
If you follow the example provided in your link you don't have to create another QTextEdit, if you want to do that you also have to place it in your UI etc..

uninitialized reference member 'Widget::handler'

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);
}

Access class from function

Is there another way besides declaring a static method inside a class to call it from outside the class ?
Here is my code (See the question marked as comment )
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
_stdcall BOOL EnumWindowsProc(HWND hw,LPARAM lp)
{
char title[255];
memset(title,0,255);
GetWindowTextA(hw,title,255);
qDebug() << title << endl;
// How can I access class MainWindow from this function ?
return true;
}
void MainWindow::on_pushButton_clicked()
{
EnumWindows(&EnumWindowsProc,0);
}
The typical way of doing this in the Windows world is to pass a pointer to the Enum function, like this:
void MainWindow::on_pushButton_clicked()
{
EnumWindows(&EnumWindowsProc,reinterpret_cast<void*>(this));
}
...and then cast it back in the callback:
_stdcall BOOL EnumWindowsProc(HWND hw,LPARAM lp)
{
char title[255];
memset(title,0,255);
GetWindowTextA(hw,title,255);
qDebug() << title << endl;
// How can I access class MainWindow from this function ?
MainWindow* that = reinterpret_cast<MainWindow*>(lp);
return true;
}