Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. a Problem with Signal/Slot

a Problem with Signal/Slot

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 987 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mHeidelberM Offline
    mHeidelberM Offline
    mHeidelber
    wrote on last edited by mHeidelber
    #1

    Hello, I am new to Qt ( not to C++ at least ) and I have a problem with making the following work:

    I defined a class that holds two integers with setters/getters in a header then declared a class instance in mainwindow.cpp .
    I want to get the user's input (two ints) after clicking on a button from a QDialog (it has it's own subclass in a seperate .cpp and .h namely dialogchangesev.h and dialogchangesev.cpp) that holds two QLineEdit , then set the properties of the class instance in mainwindow.cpp to the two inputted int.
    The problem is : I get a set of errors. ( as shown below )
    Thanks.

    Code fragments:

    // dialogchangesev.cpp
    DialogChangeSEV::DialogChangeSEV(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogChangeSEV)
    {
        ui->setupUi(this);
        connect(DialogChangeSEV, SIGNAL( sendIntData(int, int) ),
                          MainWindow, SLOT( setIntData(int,int)));
    // Error : C2275: 'DialogChangeSEV' : illegal use of this type as an expression
    // Error:   C2275: 'MainWindow' : illegal use of this type as an expression
    // see declaration of 'MainWindow'
    }
    // code goes here ...
    void DialogChangeSEV::on_setSEV_clicked()
    {
        int se, sv;
        se = ui->setSE->text().toInt();
        sv = ui->setSV->text().toInt();
    // emit sendIntData(se,sv) ;
    }
    
    //dialogchangesev.h
    // code goes here ...
    signals:
        void sendIntData(int datae, int datav);
    };
    
    //mainwindow.h
    // code goes here ...
    public slots:
        void setIntData(int datae,int datav);
    
    //mainwindow.cpp
    // code goes here ... includes and so on
    sizeEV gDimensions;
    // ...
    void MainWindow::setIntData(int datae,int datav){
        gDimensions.setSE(datae);
        gDimensions.setSV(datav);
    }
    
    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @mHeidelber said:

      connect(DialogChangeSEV, SIGNAL( sendIntData(int, int) ),
      MainWindow, SLOT( setIntData(int,int)));
      // Error : C2275: 'DialogChangeSEV' : illegal use of this type as an expression
      // Error: C2275: 'MainWindow' : illegal use of this type as an expression

      The MS compiler is correct! You can't connect classes, you can connect objects, so this should be:

       connect(this, SIGNAL(sendIntData(int, int)), parent, SLOT(setIntData(int,int)));
      

      assuming the parent is the MainWindow.

      But you should really make the connection from outside the dialog, not from its constructor, otherwise you impose on the dialog to know who's his parent/receiver, which defeats the whole purpose of the signal-slot mechanism.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • yuvaramY Offline
        yuvaramY Offline
        yuvaram
        wrote on last edited by
        #3

        HI,
        Have a glance of using signal & slots.
        http://doc.qt.io/qt-4.8/signalsandslots.html

        Yuvaram Aligeti
        Embedded Qt Developer
        : )

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved