char totalbuffer[2048] = {0};
this is the initialization of array I want. I had tried to put in main.cpp and my header file,qt creator keep show the error.
this is my .h header file code :
#ifndef QTPROJECT2_H
#define QTPROJECT2_H
#include <QDialog>
#include <QMainWindow>
#include <QtNetwork/QHostAddress>
#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>
#include <QString>
#include <QTcpSocket>
#include <QDataStream>
#include <qstring.h>
#include <QStandardItem>
namespace Ui {
class QtProject2;
}
class QtProject2 : public QDialog
{
Q_OBJECT
public:
explicit QtProject2(QWidget *parent = 0);
~QtProject2();
void start(QString address, quint16 port);
char totalbuffer[2048]={0};
QStandardItemModel* ListModel;
private slots:
void on_pushButton_clicked();
public slots:
void startTransfer();
void disconnected();
void readyRead();
signals:
void socketReady();
private:
Ui::QtProject2 *ui;
QTcpSocket *client;
};
#endif // QTPROJECT2_H
this is my main.cpp coding:
#include "qtproject2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtProject2 w;
w.show();
char totalbuffer[2048]={0};
return a.exec();
}
this is my project qtproject2.cpp:
#include "qtproject2.h"
#include "ui_qtproject2.h"
QtProject2::QtProject2(QWidget *parent) :
QDialog(parent),
ui(new Ui::QtProject2)
{
ui->setupUi(this);
}
QtProject2::~QtProject2()
{
delete ui;
QtProject2 Client2;
Client2.close();
}
void QtProject2::startTransfer()
{
ui->slabel->setText("Connected!");
}
void QtProject2::disconnected()
{
ui->slabel->setText("Disconnected!");
}
void QtProject2::readyRead()
{
client->waitForBytesWritten(1000);
client->waitForReadyRead(3000);
char CustomerData_MacAdd[][18]={"14:13:12:11:67:11","52:22:22:22:22:22", "14:22:44:55:22" };
char CustomerData_Username[][10]={"Robert","Alex","Ivan"};
QByteArray buffer1 = client->readLine();
char *temp = buffer1.data();
char buffer[1024]={0};
if (strncmp(temp,"*CLIENT",6)==0)
{
int j;
for(j=9;j<26;j++)
{ buffer[j-9]=temp[j];}
}else if(strncmp(temp,"*ALERT",5)==0)
{
int j;
for(j=8;j<25;j++)
{buffer[j-8]=temp[j];}
}
//char totalbuffer[2048]={0};
int k;
for (k=0;k<3;k++){
if (strncmp(buffer,CustomerData_MacAdd[k],16)==0){
strncat(totalbuffer,"User is in Queue : ",19);
strncat (totalbuffer,CustomerData_Username[k],size_t(CustomerData_Username[k]));
strncat(totalbuffer,"\n",2);
ui->label->setText(totalbuffer);
}}
ui->slabel->setText(buffer1);
}
void QtProject2::on_pushButton_clicked()
{
client = new QTcpSocket(this);
connect(client, SIGNAL(connected()),this, SLOT(startTransfer()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
/*Connect to User Define Port Address*/
client->connectToHost("127.0.0.1", 1234);
}
Focus for the char totalbuffer[2048]={0}; , i really no idea where i should put it.
I had searched with google and stackoverflow, all the method is using class and call in the .cpp file. but my one is GUI application,that's different with console.
What should i do? I really need a help. Appreciate the helps and thanks you guys~
EDITED***
this is the error shows.
error assigning to an array from an initializer list
I think the initialize list can't put in the header file and main.cpp so how i going to initialize an array? like C++ visual studio by using public variable....
You can use the memset or bzero functions
memset((void*)&totalbuffer, 0, sizeof(totalbuffer));
or
bzero((void*)&totalbuffer, sizeof(totalbuffer));
This is not related to Qt, but basic C++.
Class members are initialized in the constructor. You can just use std::fill there.
Related
So, I am learning my way around QThread and I understand it to a reasonable extent.
However, I am trying to implement a class derived from QThread to do a basic function, then emit a signal if a value has changed.
Let me post the Header and Source files:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "mythread.h"
#include <QTextEdit>
#include <QPushButton>
#include <QSpinBox>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
void setGUI();
void Start_Thread();
void Stop_Thread(); //will be implemented later to stop all threads
public slots:
void Update_O1(int);
private:
MyThread *m_Thread1;
QPushButton *START;
QPushButton *STOP;
QSpinBox *L1Input;
QSpinBox *L2Input; //For when a second Thread is added
QSpinBox *L3Input; //For when a third Thread is added
QTextEdit *L1Out;
QTextEdit *L2Out; //For when a second Thread is added
QTextEdit *L3Out; //For when a third Thread is added
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include <QGridLayout>
Dialog::Dialog(QWidget *parent)
: QDialog(parent), m_Thread1(new MyThread), START(new QPushButton("Start")),
STOP(new QPushButton("Stop")), L1Input(new QSpinBox), L2Input(new QSpinBox),
L3Input(new QSpinBox),L1Out(new QTextEdit),L2Out(new QTextEdit), L3Out(new QTextEdit)
{
setGUI();
connect(START, &QPushButton::clicked, this, &Dialog::Start_Thread);
connect(STOP, &QPushButton::clicked, this, &Dialog::Stop_Thread);
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int)));
//I know this is the old Syntax, but I don't know how else to do this...yet.
//I got a similar function to work on a different project using the same format
}
Dialog::~Dialog()
{
}
void Dialog::setGUI()
{
setWindowTitle("Three Threads");
resize(300,300);
START->setMaximumWidth(100);
STOP->setMaximumWidth(100);
L1Input->setRange(1,100);
L2Input->setRange(1,100);
L3Input->setRange(1,100);
L1Out->setReadOnly(true);
L2Out->setReadOnly(true);
L3Out->setReadOnly(true);
QGridLayout *GLout = new QGridLayout;
GLout->addWidget(START,0,1);
GLout->addWidget(STOP,0,2);
GLout->addWidget(L1Input,1,0);
GLout->addWidget(L2Input,1,1);
GLout->addWidget(L3Input,1,2);
GLout->addWidget(L1Out,2,0);
GLout->addWidget(L2Out,2,1);
GLout->addWidget(L3Out,2,2);
setLayout(GLout);
}
void Dialog::Start_Thread()
{
qDebug() << "START works"; //this is just to let me know the Start_Thread function is called properly
m_Thread1->start();
}
void Dialog::Stop_Thread()
{
}
void Dialog::Update_O1(int x)
{
qDebug() << QString::number(x);
}
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QObject>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void run();
void setSnum(int); //setter function
bool Stop = false; //will be used later to stop a thread
signals:
void NumberChanged(int);
private:
int Snum = 10; //temporary value. will later implement setter to use value from GUI
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QDebug>
#include <QtCore>
MyThread::MyThread(QObject *parent)
: QThread{parent}
{
}
void MyThread::run()
{
while(!(Snum == 1))
{
QMutex mutex;
mutex.lock();
if(this->Stop) break;
mutex.unlock();
if(Snum % 2 == 0)
{
Snum /= 2;
}else{
Snum *= 3;
Snum += 1;
}
emit this->NumberChanged(Snum);
}
}
void MyThread::setSnum(int startnum)
{
Snum = startnum;
}
Lastely, a very basic main.cpp file
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
The problem I am facing though is that I don't seem to be getting a signal from the emit function in mythread.cpp's run function. I am not getting an output in the qDebug() section of QtCreator.
What am I doing wrong?
I am using Qt 6.3.0.
You have this connection:
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int)));
But your method is actually called like this:
void Dialog::Update_O1(int x)
Note Update_01 vs. Update_O1. They are different characters. One is zero and the other is capital 'o'.
So, you should have:
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_O1(int)));
However, the best solution is to use the "new" signal-slot syntax.
connect(m_Thread1, &QThread::NumberChanged, this, &QDialog::Update_O1);
Actually, if the slot is short, you could even use a lambda:
connect(m_Thread1, &QThread::NumberChanged, this, [](int x) {qDebug() << QString::number(x);});
The major advantage of the new signal-slot syntax is that this sort of issue would be caught at compilation time rather than runtime. So, you would not potentially release your software with this bug.
This happens when i enter the code to the slots of my buttons
when i debug the app normally ,all functions etc everything is ok.
but when i try use my code in UI something brokes down.
I'm making the Parking System for my uni classes ,
i got one parent class vehicle and 4 child classes which inherits by public.
here's the code from the UI source file :
#include "datain.h"
#include "ui_datain.h"
#include <QMessageBox>
#include <car.h>
#include <bike.h>
#include <motorbike.h>
#include <tir.h>
#include <vehicle.h>
#include <iostream>
using namespace std;
dataIn::dataIn(QWidget *parent) :
QDialog(parent),
ui(new Ui::dataIn)
{
ui->setupUi(this);
}
dataIn::~dataIn()
{
delete ui;
}
dataIn siema;
void dataIn::on_lineEdit_textEdited(const QString &arg1)
{
siema.rejestracja=arg1;
}
void dataIn::on_lineEdit_2_textEdited(const QString &arg1)
{
siema.marka=arg1;
}
void dataIn::on_car_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Car obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_motorcycle_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Motorbike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_bike_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Bike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_tir_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Tir obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
Here's the main:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
dataIn code:
#ifndef DATAIN_H
#define DATAIN_H
#include <QString>
#include <iostream>
#include <QDialog>
using namespace std;
namespace Ui {
class dataIn;
}
class dataIn : public QDialog
{
Q_OBJECT
QString rejestracja;
QString marka;
public:
explicit dataIn(QWidget *parent = 0);
~dataIn();
private slots:
void on_lineEdit_textEdited(const QString &arg1);
void on_lineEdit_2_textEdited(const QString &arg1);
void on_car_clicked();
void on_motorcycle_clicked();
void on_bike_clicked();
void on_tir_clicked();
private:
Ui::dataIn *ui;
};
#endif // DATAIN_H
You may not declare
dataIn siema;
and you should not refer to it - remove all siema.... references from your dataIn functions (replace siema. with nothing or with this->).
When you need your dialog for the first time, create it in (and as child of) MainWindow:
void MainWindow::whateverEvent() {
dataIn *siema = new dataIn(this);
siema->show();
}
If you need to reference it again or want do prevent it from being created twice, store the pointer in a member of your MainWindow.
I created a class, whose base class was QObject, Now if I try to add a QTextBrowser in my code, I get the error as no matching function for call to QTextBrowser. I tried to compile the code by adding QWidget class but still I am unable to resolve the error. How can I resolve this.
MainWindow code
#include <QApplication>
#include "window.h"
#include "bbbserver.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
bbbServer server;
Window window;
window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}
Here is the code for window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class QProcess;
class QFile;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QTextBrowser *statusWindow;
private slots:
};
#endif // WINDOW_H
Here is the code for window.cpp
#include "window.h"
#include <QPushButton>
#include <QProcess>
#include <QTextBrowser>
#include <QDebug>
Window::Window(QWidget *parent) : QWidget(parent)
{
// Create and position the buttons on the main window
/*************** text browser *********************/
statusWindow = new QTextBrowser(this);
statusWindow->setMinimumSize(QSize(0,0));
statusWindow->setMaximumSize(QSize(10000,10000));
statusWindow->setGeometry(175, 50, 440, 420);
statusWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
}
Here is the code for bbbserver.h file
#ifndef BBBSERVER_H
#define BBBSERVER_H
#include <QObject>
#include <QWidget>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class bbbServer : public QObject
{
Q_OBJECT
public:
explicit bbbServer(QObject *parent = 0);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
};
#endif // BBBSERVER_H
this is bbbserver.cpp file
#include "bbbserver.h"
bbbServer::bbbServer(QObject *parent):
QObject(parent)
{
/*************************** SERVER *********************************/
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000))
{
qDebug() << "SERVER NOT STARTED";
}
else
{
qDebug() << "SERVER STARTED";
}
}
void bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);
socket->waitForReadyRead(30000);
qDebug() << socket->readAll();
`HERE I WANT TO ACCESS THE STATUS WINDOW(textbrowser statusWindow)`
}
And this is the full error, which is same as in screenshot.
bbbserver.cpp:8: error: no matching function for call to 'QTextBrowser::QTextBrowser(bbbServer* const)'
The parent parameter passed to the QTextBrowser constructor needs to be of type QWidget * whereas you're passing a QObject *. If you really need the bbbServer to be the parent object of the QTextBrowser then simply do...
infoSpace = new QTextBrowser;
infoSpace->QObject::setParent(this);
Note: The QObject:: qualifier is required because QWidget::setParent(QWidget *) effectively hides its base's QObject::setParent(QObject *) member meaning the latter won't take part in the normal lookup process.
I have 3 classes.
class with a mainwindow which comes from the designer(ui-file)
class wich will manage database stuff like inserts
controller class. I want to extend the whole thing to networkcommunication later.
My problem:
I want to connect a simple QPushButton ui->addbutton from the window class with a slot addEntry from the databaseclass, but I get this error :
ERROR: no matching function for call to
'generalControler::connect(QPushButton*, const char*, dbmanager*&,
const char*)'
mydb, SLOT(addEntry()));
//no difference with &mydb or *mydb
MainWindow(0x13f57988, name = "MainWindow") QPushButton(0x13f5f3e0, name = "addButton")
MainWindow(0x13f57988, name = "MainWindow") 0x13f5f3e0//<--?????
//<=Here u see the adresses printed with Qdebug(). top: mainwindowclass. bot: generalcontrolerclass
//why there is missing information after returning the adress of a 'ui->addButton' to another class? Is this maybe the problem?
main.cpp
#include <QApplication>
#include "generalcontroler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
generalControler controler;
return a.exec();
}
generalcontroler.h
#ifndef GENERALCONTROLER_H
#define GENERALCONTROLER_H
#include <QApplication>
#include "mainwindow.h"
#include "dbmanager.h"
class generalControler : public QObject
{
Q_OBJECT
public:
generalControler();
};
#endif // GENERALCONTROLER_H
generalcontroler.cpp
#include "generalcontroler.h"
#include <QDebug>
generalControler::generalControler(){
MainWindow* window = new MainWindow;
window->show();
dbmanager* mydb= new dbmanager("path_to_my_database.db", window);
mydb->addEntry();
qDebug()<<window->getThis()<<window->getcloseButton();
connect(window->getaddButton(), SIGNAL(clicked()),
mydb, SLOT(addEntry()));
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton* getaddButton();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
QPushButton* MainWindow::getaddButton()
{
return ui->addButton;
}
dbmanager.h
#ifndef DBMANAGER_H
#define DBMANAGER_H
#include <QSqlDatabase>
#include <QDebug>
#include "mainwindow.h"
class dbmanager: public QObject{
Q_OBJECT
public:
dbmanager(const QString& path);
public slots:
void addEntry();
private:
QSqlDatabase mydatabase;
};
#endif // DBMANAGER_H
dbmanager.cpp
#include "dbmanager.h"
dbmanager::dbmanager(const QString& path)
{
mydatabase = QSqlDatabase::addDatabase("QSQLITE");
mydatabase.setDatabaseName(path);
if (!mydatabase.open())
{
qDebug() << "Error: connection fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
void dbmanager::addEntry()
{
qDebug()<<"addEntry success";
}
I was searching for 6 hours but I never saw such an example with 2 classes, a controler and an ui-file. Could anyone help me?
The connect looks good to me. Try if #include <QPushButton> in generalcontroler.cpp helps. If the compiler knows about QPushButton only by forward-declaration, it doesn't know that it's a QObject and thus the connect() signatures (with QObject* in it) don't match.
I'd appreciate it, when someone could help me. I am not used to C++. I am new to it.
My problem: I want to make an object "player" usable for another class. I don't get how to archive this.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#include <QMainWindow>
#include <QMediaPlayer>
#include <QDebug>
#include "Leap.h"
#include <leapmotionlistener.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QMediaPlayer* player;
private slots:
....
private:
Ui::MainWindow *ui;
void initialize();
Controller controller;
LeapMotionListener leapMotionListener;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <iostream>
#include <QfileDialog>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
initialize();
}
QString path;
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initialize(){
controller.addListener(leapMotionListener);
leapMotionListener.setPlayer(player);
controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
}
...
leapmotionlistener.h
#ifndef LEAPMOTIONLISTENER
#define LEAPMOTIONLISTENER
#include <iostream>
#include <cstring>
#include "Leap.h"
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>
using namespace Leap;
class LeapMotionListener : public Listener {
public:
LeapMotionListener();
void setPlayer(QMediaPlayer&);
//... some more methods
private:
QMediaPlayer player;
};
#endif // LEAPMOTIONLISTENER
leapmotionlistener.cpp
//calls the player object in one method like the following
player.stop();
My main questions are: What am I doing wrong while referencing and instantiating? And how can leapmotionlistener access the same player object (I want to influence the audio)?
I get the Error:
MusicPlayer\mainwindow.cpp:32: Error: C2664: 'void LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument 1 'QMediaPlayer *' in 'QMediaPlayer &' not possible
This little project is downloadable for a quick view with the following link:
https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0
Thanks in advance!
All you need to do is
leapMotionListener.setPlayer(*player);
An lvalue reference binds to an object, not to the pointer to the object. In order to get the object from a pointer to it, you need to dereference the pointer using the *.
Among other problems, the QMediaPlayer you're trying to pass to setPlayer is a pointer, not a reference to an object.
You can either instantiate your player object like this:
QMediaPlayer player;
Or you can change your setPlayer function signature to this:
void setPlayer(QMediaPlayer* mplayer);
If you update your function signature, you'll also need fix other function signatures and leave the ampersands out of your connect() statements when you're referencing the player.
the following worked now:
in MainWindow.h:
public:
QMediaPlayer* player;
in MainWindow.cpp:
player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
in LeapMotionListener.h:
private:
QMediaPlayer *player;
public:
void setPlayer(QMediaPlayer *mplayer);
in LeapMotionListener.cpp:
LeapMotionListener::LeapMotionListener()
: player(0)
{}
void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
player = mplayer;
}