Question

Hi i have made a gui in qt4 designer and want to add custom slots with custom class. It project compiles nicely without errors but my custom function wont work what am i doing wrong? I will show u the header file qt4 designer made for me and ill show u my custom file as well as the main.cpp.. first the main.cpp

I have revised my code, here is what i have now i have added a file called sweetest.cpp and edited the sweetest.h here are my new file and the error i recieve..

First main.cpp

#include "ui_sweetguiform.h"

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QWidget *widget = new QWidget;
     Ui::SweetGuiForm ui;
     ui.setupUi(widget);

     widget->show();
     return app.exec();
 }

now my custom header file sweetest.cpp

#include "sweetest.h"
// trying to include the .moc file wouldnt work at all.

now the sweettest.h file with my code

#include "ui_sweetguiform.h"

class SweetGuiForm : public QWidget
{
    Q_OBJECT
public:
    SweetGuiForm( ): ui( new Ui::SweetGuiForm )
    {
        ui->setupUi( this );
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
    }

public slots:
    void on_buttonBox_accepted()
    {
         ui.textEdit->setText(QString::number(23));

    }

protected:
    Ui::SweetGuiForm* ui;
};

Here is the compile error i recieve.. I am really stuck

In file included from sweetest.cpp:1:
sweetest.h: In member function ‘void SweetGuiForm::on_buttonBox_accepted()’:
sweetest.h:16: error: request for member ‘textEdit’ in ‘((SweetGuiForm*)this)->SweetGuiForm::ui’, which is of non-class type ‘Ui::SweetGuiForm*’
make: *** [sweetest.o] Error 1

I think im getting closer

Was it helpful?

Solution 2

Ok guys i figured it out and thought ide share what i found. First the documentation is excellent in qt4 use it.

I found you can use qt4 designer to generate the hearder files, first i complied it with out custom slots and generated the ui_sweetgui2.h, then i could open the sweetgui2.h file generated by the first compile i did delete what qt4 put in there and put my custom slots in at that stage. did my head in for hours.... days. so here is the simplest app on earth but its got me started so here are the files and code that worked for me and the documentation basically got me to click on to whats going on.

main.cpp Strait from the documentation just changed the class name "SweetGuiForm".

 #include <QApplication>
 #include "sweetgui2.h"
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     SweetGuiForm sweetgui;
     sweetgui.show();
     return app.exec();
 } 

next sweetgui2.cpp My first attempt at c++.. ugly but works. But again i found everything about getting the text from the textEdit and type casting it to a int in the calculator example and searching for toPlainText() in the qt4 assistant. notice below im including the file that i will define the new slots that ill show further on in my explanation. hope im making sense.

#include <QtGui>
#include "sweetgui2.h"
    SweetGuiForm::SweetGuiForm(QWidget *parent)
: QWidget(parent)
{
    ui.setupUi(this);
}

void SweetGuiForm::on_buttonBox_accepted()
{
    QString stringamount = ui.textEdit->toPlainText();
    int digitamount = stringamount.toInt();
    ui.textEdit->setText(QString::number(25 + digitamount));
}

next sweetgui2.h the one we included above My custom header file with my custom slot.... simple as i said from the calculator example and twisted a lil.. you will get it this is not what it looks like when you generate it from designer on the first compile this is after i have deleted nearly all what was there and opened the calculator example and followed in the tutorial wich shows you how to make your first custom slot .

#ifndef SWEETGUI2_H
#define SWEETGUI2_H
#include "ui_sweetgui2.h"
class SweetGuiForm : public QWidget
{
    Q_OBJECT
public:
SweetGuiForm(QWidget *parent = 0);
private slots:
    void on_buttonBox_accepted();
private:
Ui::SweetGuiForm ui;
};
#endif // SWEETGUI2_H

Again Straight from the documentation. I used the calculator example to get the basic flow.

next ui_sweetgui2.h I include this file because i was trying to add my slots to the sweetgui2.h that was generated by qt4 desinger. doesnt work guys ..so i compiled first with sweetgui2.h file you generate with the designer, i go to forms menu then view code that is where u can save header files. then of course save the ui file. and compile then you end up with the ui_sweetgui2.h file wich looked like the sweetgui2.h generated by the designer

#ifndef UI_SWEETGUI2_H
#define UI_SWEETGUI2_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_SweetGuiForm
{
public:
    QDialogButtonBox *buttonBox;
    QTextEdit *textEdit;

    void setupUi(QWidget *SweetGuiForm)
    {
        if (SweetGuiForm->objectName().isEmpty())
            SweetGuiForm->setObjectName(QString::fromUtf8("SweetGuiForm"));
        SweetGuiForm->resize(486, 238);
        buttonBox = new QDialogButtonBox(SweetGuiForm);
        buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
        buttonBox->setGeometry(QRect(150, 200, 181, 26));
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
        textEdit = new QTextEdit(SweetGuiForm);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));
        textEdit->setGeometry(QRect(150, 50, 221, 91));

        retranslateUi(SweetGuiForm);
        QObject::connect(buttonBox, SIGNAL(rejected()), SweetGuiForm, SLOT(close()));

        QMetaObject::connectSlotsByName(SweetGuiForm);
    } // setupUi

    void retranslateUi(QWidget *SweetGuiForm)
    {
        SweetGuiForm->setWindowTitle(QApplication::translate("SweetGuiForm", "SweetGuiBack", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class SweetGuiForm: public Ui_SweetGuiForm {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_SWEETGUI2_H 

Then i recompiled again with my custom slots and shazam! now i can begin to learn some c++. thanks for all the hints guys, between you all and the documentation i got there. hope this helps. The main thing to look at is the order things are included i mean my sweetgui2.cpp file includes the sweetgui2.h file. wich grabs all my custom stuff.

My sweetgui2.h file includes the ui_sweetgui2.h wich has all the stuff the designer made when i did the first compile. Main.cpp calls my SweetGuiForm class .

As you all can see my first couple days with c++ but this is a good starting point. it made me put the basic flow together in my mind. qt4 assistant look at it.. its well explained and the examples seem very good. ho ho ho merry xmas. hope my explanation helps.

OTHER TIPS

The way that signals and slots work is that you must connect a signal to a slot. In your code, the simplest way to do that is in the constructor for the SweetGuiForm. You need to add:

SweetGuiForm() : ui(new Ui::SweetGuiForm) {
  ui->setupUi(this);
  connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
}

When the buttonBox emits its accepted signal all slots connected to it will be called.

update 1

On further inspection of your code, you are also missing the Qt macros that are used by the Qt MOC (meta-object compiler) system (http://qt-project.org/doc/qt-4.8/moc.html):

class SweetGuiForm : public QWidget
{
  Q_OBJECT
  public:
  ...
};

You also have to push the code through the MOC tool. It will generate a source file that needs to be included in your source. As I recall, you must include that in a cpp file; inclusion in a header is problematic. The following should be sufficient:

sweetguiform.cpp:

#include "suiteguiform.h"
#include "sweetguiform.moc"

update 2

On further further reflection, I had forgotten about the automatic signal/slot connection feature when you name your slots using special names (such as on_buttonBox_accepted). There is a blog post on just that here: http://qtway.blogspot.com/2010/08/automatic-connections-using-qt-signals.html. I've not used it myself, so I can't comment on its ability to work when using a ui member variable, though I suspect that it does not work in that arrangement. Regardless, you still need the Q_OBJECT macro and MOC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top