Results 1 to 3 of 3

Thread: Problem with passing Pointers to a Slot

  1. #1
    Join Date
    May 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Question Problem with passing Pointers to a Slot

    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:

    Qt Code:
    1. //main.cpp
    2.  
    3. #include <QApplication>
    4. #include <QFont>
    5. #include <QPushButton>
    6. #include <QWidget>
    7. #include <qwt_plot.h>
    8. #include <qwt_plot_curve.h>
    9. #include <QFileDialog>
    10. #include <QStringList>
    11. #include <QGridLayout>
    12. #include <QVBoxLayout>
    13. #include <qwt_plot_grid.h>
    14. #include <qwt_legend.h>
    15. #include <QDebug>
    16. #include <QObject>
    17. #include "button.h"
    18.  
    19. int main(int argc, char *argv[])
    20. { const int anz=50;
    21. double *x1;
    22. double *y1;
    23. double *x2;
    24. double *y2;
    25.  
    26. x1 = new double[50];
    27. y1 = new double[50];
    28. x2 = new double[50];
    29. y2 = new double[50];
    30.  
    31. for (int i=0;i<anz;i++){
    32. x1[i]= i;
    33. y1[i]= 3*i;
    34. x2[i]= i;
    35. y2[i]= 2*i;
    36. }
    37.  
    38.  
    39. QApplication app(argc, argv);
    40. QWidget window;
    41. window.resize(800, 800);
    42.  
    43.  
    44. OpenFile *open = new OpenFile("Open", &window);
    45.  
    46. QPushButton quit("Quit", &window);
    47. quit.setFont(QFont("Times", 18, QFont::Bold));
    48. quit.setGeometry(10, 450, 100, 40);
    49. QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
    50.  
    51. QPushButton copy("Copy", &window);
    52. copy.setFont(QFont("Times", 18, QFont::Bold));
    53. copy.setGeometry(150, 450, 100, 40);
    54. QObject::connect(&copy, SIGNAL(clicked()), open, SLOT(CopyArray(x1,y1)));
    55.  
    56. QwtPlot *myPlot = new QwtPlot(&window);
    57.  
    58. .
    59. .
    60. .
    61.  
    62. QGridLayout *grid2 = new QGridLayout;
    63. grid2->addWidget(myPlot, 0,0,4,4);
    64. grid2->addWidget(&quit, 5,0);
    65. grid2->addWidget(open, 5,3);
    66. grid2->addWidget(&copy, 5,1);
    67.  
    68. window.setLayout(grid2);
    69. window.show();
    70. return app.exec();
    71. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // button.h
    2.  
    3. #ifndef BUTTON_H
    4. #define BUTTON_H
    5.  
    6. #include <QApplication>
    7. #include <QFont>
    8. #include <QPushButton>
    9. #include <QWidget>
    10. #include <QFileDialog>
    11. #include <QStringList>
    12. #include <QDebug>
    13. #include <QObject>
    14. #include <QFile>
    15.  
    16. class OpenFile:public QPushButton
    17. {
    18. Q_OBJECT
    19. public:
    20. OpenFile(QString name, QWidget * parent);
    21.  
    22. public slots:
    23. void CopyArray(double* xe, double* ye);
    24.  
    25. private:
    26. double *x, *y;
    27.  
    28. private slots:
    29. void on_pushButton_clicked();
    30. };
    31.  
    32.  
    33.  
    34. #endif // button.h
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //button.cpp
    2.  
    3. #include "button.h"
    4.  
    5. OpenFile::OpenFile(QString name, QWidget * parent)
    6. :QPushButton(name, parent)
    7. {
    8. setFont(QFont("Times", 18, QFont::Bold));
    9. setGeometry(150, 450, 100, 40);
    10. QObject::connect(this, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
    11. }
    12.  
    13. void OpenFile::on_pushButton_clicked()
    14. {
    15. QString fileName = QFileDialog::getOpenFileName(this, tr("Open TXT File"),
    16. "",
    17. tr("TXT file (*.txt)"));
    18.  
    19. qDebug() << fileName;
    20.  
    21. QFile file( fileName );
    22. if( !file.open( QIODevice::ReadOnly ) ) //open file for reading
    23. return; //error
    24.  
    25. QString fileData = file.readAll(); //read all to byte array
    26.  
    27. file.close(); //close file
    28.  
    29. QStringList strList;
    30. strList = fileData.split( "," ); //create number list as strings
    31.  
    32. qDebug() << strList << strList.count(); //print qtway what's inside baData
    33.  
    34. QList< double > dDoubleList;
    35.  
    36. bool ok;
    37.  
    38. for( int i = 0; i < strList.count(); ++i )
    39. {
    40. dDoubleList.append( strList[i].toInt( &ok ) );
    41. qDebug() << ok; //see if error - false = error , true = OK
    42. }
    43.  
    44. this->x = new double[strList.count()];
    45. this->y = new double[strList.count()];
    46. int j=0;
    47. for (int i=0; i< strList.count(); i=i+2)
    48. { this->x[j]=dDoubleList[i];
    49. this->y[j]=dDoubleList[i+1];
    50. j++;
    51. }
    52.  
    53. qDebug() << x[0] << y[0] << x[1] << y[1];
    54. }
    55.  
    56. void OpenFile::CopyArray(double* xe, double* ye)
    57. {
    58. xe = this->x;
    59. ye = this->y;
    60. qDebug() << xe[0] << ye[0] << xe[1] << ye[1];
    61. }
    To copy to clipboard, switch view to plain text mode 


    Maybe someone has an idea what im doing wrong?

    Regards
    Basti

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem with passing Pointers to a Slot

    The number of arguments in your slot may not be more than the number of arguments in your signal

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with passing Pointers to a Slot

    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.
    Last edited by high_flyer; 26th May 2010 at 15:45.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Passing an integer to a slot
    By bizmopeen in forum Newbie
    Replies: 6
    Last Post: 30th October 2009, 09:51
  2. pointers behaviour in qt and specially signals/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2008, 19:34
  3. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 21:55
  4. Problems passing an array of pointers to objects to a function
    By Valheru in forum General Programming
    Replies: 16
    Last Post: 30th June 2008, 00:11
  5. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.