PDA

View Full Version : Problem with passing Pointers to a Slot



Basti300
26th May 2010, 14:24
Hello People

My Problem probably is very simple but i cant find a solution and i hope someone here can tell me what i do wrong.
Well i made a Class that creates a PushButton and connects that PushButton to a private Slot that reads a File and converts the Data to double arrays.
Then i created a Pushbutton in my main function that connects to that class and calls a public Slot passing the Arrays where i want the data to be stored.
Compiling works fine but when i run the programm the debug console tells me:

Object::connect: No such slot OpenFile::CopyArray(x1, y1)

Here is my source code:



//main.cpp

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <QFileDialog>
#include <QStringList>
#include <QGridLayout>
#include <QVBoxLayout>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <QDebug>
#include <QObject>
#include "button.h"

int main(int argc, char *argv[])
{ const int anz=50;
double *x1;
double *y1;
double *x2;
double *y2;

x1 = new double[50];
y1 = new double[50];
x2 = new double[50];
y2 = new double[50];

for (int i=0;i<anz;i++){
x1[i]= i;
y1[i]= 3*i;
x2[i]= i;
y2[i]= 2*i;
}


QApplication app(argc, argv);
QWidget window;
window.resize(800, 800);


OpenFile *open = new OpenFile("Open", &window);

QPushButton quit("Quit", &window);
quit.setFont(QFont("Times", 18, QFont::Bold));
quit.setGeometry(10, 450, 100, 40);
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

QPushButton copy("Copy", &window);
copy.setFont(QFont("Times", 18, QFont::Bold));
copy.setGeometry(150, 450, 100, 40);
QObject::connect(&copy, SIGNAL(clicked()), open, SLOT(CopyArray(x1,y1)));

QwtPlot *myPlot = new QwtPlot(&window);

.
.
.

QGridLayout *grid2 = new QGridLayout;
grid2->addWidget(myPlot, 0,0,4,4);
grid2->addWidget(&quit, 5,0);
grid2->addWidget(open, 5,3);
grid2->addWidget(&copy, 5,1);

window.setLayout(grid2);
window.show();
return app.exec();
}



// button.h

#ifndef BUTTON_H
#define BUTTON_H

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QFileDialog>
#include <QStringList>
#include <QDebug>
#include <QObject>
#include <QFile>

class OpenFile:public QPushButton
{
Q_OBJECT
public:
OpenFile(QString name, QWidget * parent);

public slots:
void CopyArray(double* xe, double* ye);

private:
double *x, *y;

private slots:
void on_pushButton_clicked();
};



#endif // button.h



//button.cpp

#include "button.h"

OpenFile::OpenFile(QString name, QWidget * parent)
:QPushButton(name, parent)
{
setFont(QFont("Times", 18, QFont::Bold));
setGeometry(150, 450, 100, 40);
QObject::connect(this, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}

void OpenFile::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open TXT File"),
"",
tr("TXT file (*.txt)"));

qDebug() << fileName;

QFile file( fileName );
if( !file.open( QIODevice::ReadOnly ) ) //open file for reading
return; //error

QString fileData = file.readAll(); //read all to byte array

file.close(); //close file

QStringList strList;
strList = fileData.split( "," ); //create number list as strings

qDebug() << strList << strList.count(); //print qtway what's inside baData

QList< double > dDoubleList;

bool ok;

for( int i = 0; i < strList.count(); ++i )
{
dDoubleList.append( strList[i].toInt( &ok ) );
qDebug() << ok; //see if error - false = error , true = OK
}

this->x = new double[strList.count()];
this->y = new double[strList.count()];
int j=0;
for (int i=0; i< strList.count(); i=i+2)
{ this->x[j]=dDoubleList[i];
this->y[j]=dDoubleList[i+1];
j++;
}

qDebug() << x[0] << y[0] << x[1] << y[1];
}

void OpenFile::CopyArray(double* xe, double* ye)
{
xe = this->x;
ye = this->y;
qDebug() << xe[0] << ye[0] << xe[1] << ye[1];
}



Maybe someone has an idea what im doing wrong?

Regards
Basti

tbscope
26th May 2010, 14:29
The number of arguments in your slot may not be more than the number of arguments in your signal

high_flyer
26th May 2010, 14:45
You are connecting the signals and slots wrong.
Read the QObject::coonect() docs.
You are passing instances instead of types.

In addition you are connecting slots that expect input with signals that deliver none.