[SOLVED] Qt Events



  • Hi there. I'm using Qt's drag and drop method and I want to know how do I create event handlers for events such as button clicks, combo box changes etc. I'm new to this so be nice! :D

    Thanks!


  • Lifetime Qt Champion

    Hi,

    What exactly do you want do drag and drop ?

    Did you take a look at the "Drag And Drop" chapter of Qt's documentation ?



  • I've sorted the events side, but have come across this issue;

    error: uninitialized reference member 'Widget::handler' [-fpermissive]
    Widget::Widget(QWidget *parent) :
    ^

    this is after adding an event handler for "on_comboBox_currentIndexChanged(int index)
    "



  • Hi,
    You do not need an eventhandler for any widget that is clicked/changed or what ever you want to do with it. In basic Qt has two way of doing things. First is the event handler catch way (like the drag and drop events). You catch the event when the OS/Qt issues it in a certain widget.
    The other is to connect widgets modify/edit/change and all other kind of widgets to slots. This is called the Signal/Slot method. This is well documented in the Qt docs.
    What you described in your second post is a Signal/Slot member function going bad. Show the code of your slot and we might be able to help.



  • this is the code;
    @
    void Widget::on_comboBox_currentIndexChanged(int index)
    {
    for(std::vectorSDI::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)
    {
    Widget::dataInsertion(ship);
    }
    }

    }

    and the header file for widget;

    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include "shipHandler.h"

    namespace Ui
    {
    class Widget;
    }

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    explicit Widget(QWidget parent = 0);
    ~Widget();
    void populateCombo();
    void getList(SDI::shipHandler& shipHandler);
    void dataInsertion(SDI::navalVessels
    ship);
    private slots:

    void on_comboBox_currentIndexChanged(int index);
    
    void on_pushButton_clicked();
    

    private:
    Ui::Widget *ui;
    SDI::shipHandler& handler;
    };

    #endif // WIDGET_H
    @

    [edit: add missing @ coding tags SGaist]


  • Lifetime Qt Champion

    @if (str == qstr)
    {
    Widget::dataInsertion(ship); << You are calling it like it's static function which it's not
    }
    @

    @
    private:
    Ui::Widget *ui;
    SDI::shipHandler& handler; << is a reference. Why ?
    @



  • Hi,
    Do you set the SDI::shipHandler& handler to a valid value in the constructor of you Widget class??? The compiler bugs you that you are using an uninitialized variable. The on_comboBox_ function is probably the first that uses that variable.



  • I've sorted this error, it was because i was assigning a reference not initialising it. Thank you for your time people



  • Please place [SOLVED] in front of your first post!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.