Qt textfinder tutorial issue



  • Hello, im a little (Or a big) noob to this -QT- Qt framework, and i trying to understand how it works. I was trying the Textfinder tutorial in -QT- Qt Creator, but without any succes. I have done all the tasks, but in the end, Im getting thís error:

    @
    D:\QtSDK\Examples\TextFinder-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug..\TextFinder\textfinder.cpp:19: error: no 'void TextFinder::on_findButton_clicked()' member function declared in class 'TextFinder'
    @

    My textfinder.cpp is looking like this:

    @
    #include "textfinder.h"
    #include "ui_textfinder.h"
    #include <QtCore/QFile>
    #include <QtCore/QTextStream>

    TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TextFinder)
    {
    ui->setupUi(this);
    loadTextFile();
    }

    TextFinder::~TextFinder()
    {
    delete ui;
    }

    void TextFinder::on_findButton_clicked()
    {
    QString searchString = ui->lineEdit->text();
    ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
    QMetaObject::connectSlotsByName(TextFinder);
    }

    void TextFinder::loadTextFile()
    {
    QFile inputFile(":/input.txt");
    inputFile.open(QIODevice::ReadOnly);

     QTextStream in(&inputFile&#41;;
     QString line = in.readAll();
     inputFile.close();
    
     ui->textEdit->setPlainText(line);
     QTextCursor cursor = ui->textEdit->textCursor();
     cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    

    }
    @

    Can someone explain what i had done wrong, and i apoligize for my bad english.

    [EDIT: code formatting, please wrap in @-tags, Volker]



  • You most probably forgot to add on_findButton_clicked() as a public slot in your class header file textfinder.h

    It should look something like this:

    @
    class TextFinder : public QWidget
    {

    Q_OBJECT

    public:
    // your constructors etc. here

    protected slots:
    void on_findButton_clicked();

    // ... other stuff ...
    };
    @



  • This is my header file:

    @
    #ifndef TEXTFINDER_H
    #define TEXTFINDER_H

    #include <QWidget>

    namespace Ui {
    class TextFinder;
    }

    class TextFinder : public QWidget
    {
    Q_OBJECT

    public:
    explicit TextFinder(QWidget *parent = 0);
    ~TextFinder();

    private slots:

    void on_FindButton_clicked();
    

    private:
    Ui::TextFinder *ui;
    void loadTextFile();
    };

    #endif // TEXTFINDER_H
    @


  • Moderators

    Again, please put @ tags around your code samples.

    (Above post edited to make @-tag correction.)



  • on_findButton_clicked != on_FindButton_clicked

    (f vs. F in find vs. Find)



  • Sorry, im all new to this. But it helped, thanks a lot.



  • You're welcome. Please always keep in mind that C++ (and C) are case sensitive languages, so a variable x is not the same a variable X. Also this affects the autoconnection, so you should double check the exact spelling of your widget in the UI (upper or lower case F).

    BTW: If you use Qt Creator as your IDE, just go to the header file, put the cursor on a method name and press alt-return, this will add a stub implementation in your .cpp file with all the spelling and upper/lower case done right :)



  • Hi,
    I'm just starting too. At this point in time it seems the following will work

    @QMetaObject::connectSlotsByName(this);@

    The most reasonable place to place it is in the TextFinder constructor, so it looks like this:

    @TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TextFinder)
    {
    ui->setupUi(this);
    loadTextFile();
    QMetaObject::connectSlotsByName(this);
    }@

    I am not sure why would you want it in a clicked event and no one corrected you. :)

    Using version
    Qt Creator 2.4.1
    Based on Qt 4.8.0 (64 bit)
    Built on Mar 21 2012 at 22:53:22



  • [quote author="Byro" date="1343928612"]
    The most reasonable place to place it is in the TextFinder constructor, so it looks like this:
    @TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TextFinder)
    {
    ui->setupUi(this);
    loadTextFile();
    QMetaObject::connectSlotsByName(this);
    }@[/quote]

    Yeah, thanks Byro! It works. In the Tutorial is not written where to place the QMetaObject::
    Also they want the className "TextFinder" - this dosn't work.

    This is my first work with Qt - I looked for the mistake half the night. :-(
    Now I'm HAPPY!

    @Volker: Thanks for your advice! Sounds very useful. :-)

    Sorry, I hope you'll understand my bad english - had been a bad teacher in school - 43 years ago. :-)


Log in to reply
 

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