HILFE!! was mach ich falsch?

Alles rund um die Programmierung mit Qt
Antworten
jansie
Beiträge: 11
Registriert: 28. September 2007 16:03

HILFE!! was mach ich falsch?

Beitrag von jansie »

Hallo.. ich habe in den letzten Tagen mit Qt-Programmierung angefangen. Ich kann schon eine Form im designer machen und das ganze compilieren... aber mehr auch nicht. (naja Grundkenntnisse von C++ hab ich).. Ich habe nun einen Button und wenn ich draufklicke soll in der Konsole die Meldung "Button pressed" erscheinen.. aber das klappt nicht :(

2007.cpp

Code: Alles auswählen

#include <qapplication.h>
#include <iostream>

#include "test2007.h"

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

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

void Ui_Form::testaktion() {
std::cout << "Button pressed";
}
test2007.h

Code: Alles auswählen

/********************************************************************************
** Form generated from reading ui file 'test2007.ui'
**
** Created: Fri Sep 28 15:47:28 2007
**      by: Qt User Interface Compiler version 4.2.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/

#ifndef UI_TEST2007_H
#define UI_TEST2007_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>

class Ui_Form
{
public:
    QPushButton *pushButton;
    QPushButton *pushButton_2;

    void setupUi(QWidget *Form)
    {
    Form->setObjectName(QString::fromUtf8("Form"));
    pushButton = new QPushButton(Form);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));
    pushButton->setGeometry(QRect(10, 10, 231, 121));
    pushButton_2 = new QPushButton(Form);
    pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    pushButton_2->setGeometry(QRect(240, 160, 141, 81));

    retranslateUi(Form);

    QSize size(414, 282);
    size = size.expandedTo(Form->minimumSizeHint());
    Form->resize(size);

    QObject::connect(pushButton, SIGNAL(clicked()), Form, SLOT(close()));
    QObject::connect(pushButton_2, SIGNAL(clicked()), Form, SLOT(testaktion()));

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

    void retranslateUi(QWidget *Form)
    {
    Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
    pushButton->setText(QApplication::translate("Form", "Dr\303\274ck mich!!!", 0, QApplication::UnicodeUTF8));
    pushButton_2->setText(QApplication::translate("Form", "Mich auch!!!", 0, QApplication::UnicodeUTF8));
    Q_UNUSED(Form);
    } // retranslateUi

public slots:
void testaktion();

};

namespace Ui {
    class Form: public Ui_Form {};
} // namespace Ui

#endif // UI_TEST2007_H
Was mach ich falsch? :(
QNoob
Beiträge: 10
Registriert: 13. September 2007 00:35

Beitrag von QNoob »

Moin,

hab zwar auch noch nicht so viel Erfahrung mit Qt aber die Art und Weise in der du das programmiert hast ist nicht richtig denke ich.

Du musst für dein Formblatt eine neue Klasse erstellen z.B. TestForm

testform.h:

Code: Alles auswählen

#ifndef TESTFORM_H
#define TESTFORM_H

#include "test2007.h"

class TestForm : public QWidget
{
  Q_OBJECT

  public:
    TestForm (QWidget *parent = 0);
   
  public slots:
    void testaktion();
  
  private:
    Ui::Form ui;
};

#endif

testform.cpp:

Code: Alles auswählen

#include <iostream>
#include "testform.h"

TestForm::TestForm (QWidget *parent) : QWidget (parent)
{
  ui.setupUi (this);
}

void TestForm::testaktion() {
   std::cout << "Button pressed\n";
} 
main.cpp:

Code: Alles auswählen

#include <qapplication.h>

#include "testform.h"

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   
   TestForm window;
   
   window.show();
   
   return app.exec();
}
probiers mal damit :-)
jansie
Beiträge: 11
Registriert: 28. September 2007 16:03

...

Beitrag von jansie »

Erstmal danke.. aber...
nun bekomme ich den Fehler:

Code: Alles auswählen

2007.o: In function `main':
2007.cpp:(.text+0x49): undefined reference to `TestForm::TestForm(QWidget*)'
2007.cpp:(.text+0x66): undefined reference to `vtable for TestForm'
2007.cpp:(.text+0x6f): undefined reference to `vtable for TestForm'
2007.cpp:(.text+0xad): undefined reference to `vtable for TestForm'
2007.cpp:(.text+0xb6): undefined reference to `vtable for TestForm'
collect2: ld gab 1 als Ende-Status zurück
2007.cpp ist meine main.cpp :-)
:-(
QNoob
Beiträge: 10
Registriert: 13. September 2007 00:35

Beitrag von QNoob »

mach mal ein

Code: Alles auswählen

 make distclean 
und dann nochmal

Code: Alles auswählen

qmake -project
qmake
make
kann auch sein, dass nur

Code: Alles auswählen

qmake
reicht
jansie
Beiträge: 11
Registriert: 28. September 2007 16:03

Beitrag von jansie »

joo.. jetzt gehts :-D danke danke danke...
aber woran lag das?
QNoob
Beiträge: 10
Registriert: 13. September 2007 00:35

Beitrag von QNoob »

Der moc musste erst noch deine testform.cpp datei parsen und das passiert erst bei einem erneuten qmake
jansie
Beiträge: 11
Registriert: 28. September 2007 16:03

Beitrag von jansie »

:-) gut.. also nochmal danke ;-)
Antworten