October 19, 2011

Cannabis2011 Cannabis2011
Lab Rat
12 posts

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:

  1. 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:

  1. #include "textfinder.h"
  2. #include "ui_textfinder.h"
  3. #include <QtCore/QFile>
  4. #include <QtCore/QTextStream>
  5.  
  6. TextFinder::TextFinder(QWidget *parent) :
  7.     QWidget(parent),
  8.     ui(new Ui::TextFinder)
  9. {
  10.     ui->setupUi(this);
  11.     loadTextFile();
  12. }
  13.  
  14. TextFinder::~TextFinder()
  15. {
  16.     delete ui;
  17. }
  18.  
  19. void TextFinder::on_findButton_clicked()
  20.  {
  21.     QString searchString = ui->lineEdit->text();
  22.          ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
  23.           QMetaObject::connectSlotsByName(TextFinder);
  24.  }
  25.  
  26. void TextFinder::loadTextFile()
  27.  {
  28.      QFile inputFile(":/input.txt");
  29.      inputFile.open(QIODevice::ReadOnly);
  30.  
  31.      QTextStream in(&inputFile);
  32.      QString line = in.readAll();
  33.      inputFile.close();
  34.  
  35.      ui->textEdit->setPlainText(line);
  36.      QTextCursor cursor = ui->textEdit->textCursor();
  37.      cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
  38.  }

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

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

8 replies

October 19, 2011

Volker Volker
Ant Farmer
5331 posts

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:

  1. class TextFinder : public QWidget
  2. {
  3.  
  4. Q_OBJECT
  5.  
  6. public:
  7.    // your constructors etc. here
  8.  
  9. protected slots:
  10.     void on_findButton_clicked();
  11.  
  12. // ... other stuff ...
  13. };

October 19, 2011

Cannabis2011 Cannabis2011
Lab Rat
12 posts

This is my header file:

  1. #ifndef TEXTFINDER_H
  2. #define TEXTFINDER_H
  3.  
  4. #include <QWidget>
  5.  
  6. namespace Ui {
  7.     class TextFinder;
  8. }
  9.  
  10. class TextFinder : public QWidget
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit TextFinder(QWidget *parent = 0);
  16.     ~TextFinder();
  17.  
  18.  
  19. private slots:
  20.  
  21.     void on_FindButton_clicked();
  22.  
  23.  
  24. private:
  25.     Ui::TextFinder *ui;
  26.     void loadTextFile();
  27. };
  28.  
  29. #endif // TEXTFINDER_H

October 19, 2011

mlong mlong
Robot Herder
1537 posts

Again, please put @ tags around your code samples.

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

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

October 19, 2011

Volker Volker
Ant Farmer
5331 posts

on_findButton_clicked != on_FindButton_clicked

(f vs. F in find vs. Find)

October 19, 2011

Cannabis2011 Cannabis2011
Lab Rat
12 posts

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

October 19, 2011

Volker Volker
Ant Farmer
5331 posts

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 :)

August 2, 2012

Byro Byro
Lab Rat
1 posts

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

  1. QMetaObject::connectSlotsByName(this);

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

  1. TextFinder::TextFinder(QWidget *parent) :
  2.     QWidget(parent),
  3.     ui(new Ui::TextFinder)
  4. {
  5.     ui->setupUi(this);
  6.     loadTextFile();
  7.     QMetaObject::connectSlotsByName(this);
  8. }

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

April 23, 2013

lypo lypo
Lab Rat
1 posts
Byro wrote:
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); }@

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. :-)

 
  ‹‹ QTableView::clearSpans()      Running QProcess in Thread to not freeze GUI on waitForFinished() ››

You must log in to post a reply. Not a member yet? Register here!