April 10, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Help with QTextEdit.

Page  
1

So, I’m trying to follow this tutorial in Visual C++ 2010, only the only thing I would say is, “Hello World!”

http://wiki.forum.nokia.com/index.php/How_to_use_QSplitter_and_QTextEdit_in_Qt

I recreated the splitter.h from here since it appears to need that:
http://stackoverflow.com/questions/3888611/qt-separator-widget

…&, of course, it’s not working. Anyone wanaa help? You can download the source here:
http://www.4shared.com/file/z-W7FIJF/Project_Is_This_a_Bad_Game.html

Thank you,
M260

27 replies

April 10, 2011

Volker Volker
Ant Farmer
5428 posts

First a hint for posting:

Thanks for providing the source code, it helps in searching the problem. But please do not include object and build files for your platform/compiler/IDE. Your download is about 18 MB, which is quite big for four sources files in the end ;-)

It makes your life much more easier and will most probably lead to more people reading your post and thinking about your problem.

So, regarding your actual problem. This is nothing directly Qt related, more sort of some general issues with C++. I suppose you are new to C++, as these kind of errors are very common, every programmer stumbles over them once in a while, even the most skilled ones.

I’ll post the code here to ease the analysis:

—- splitter.h —-

  1. #ifndef SPLITTER_H
  2. #define SPLITTER_H
  3.  
  4. #include <QtGui/QDialog>
  5.  
  6. class splitter : public QDialog
  7. {
  8.     Q_OBJECT;
  9.  
  10.     QWidget*        widget1;
  11.     QWidget*        widget2;
  12.  
  13.     QPushButton*    button;
  14.  
  15. public:
  16.     splitter(QWidget *parent = 0, Qt::WFlags flags = 0);
  17.     ~splitter();
  18.  
  19. private slots:
  20.     void    showHide(void);
  21. };
  22.  
  23. #endif // SPLITTER_H

—- projectisthisabadgame.cpp —-

  1. #include "projectisthisabadgame.h"
  2. #include "splitter.h"
  3. #include<QSplitter>
  4. #include<QTextEdit>
  5. #include<QHBoxLayout>
  6.  
  7. ProjectIsThisaBadGame::ProjectIsThisaBadGame(QWidget *parent, Qt::WFlags flags)
  8.  : QMainWindow(parent, flags)
  9. {
  10.  ui.setupUi(this);
  11. }
  12.  
  13. ProjectIsThisaBadGame::~ProjectIsThisaBadGame()
  14. {
  15.  
  16. }
  17.  
  18. splitter::splitter(QWidget *parent)
  19.     : QWidget(parent)
  20. {
  21.  QHBoxLayout *layout=new QHBoxLayout(this);
  22.  QTextEdit *editor1 = new QTextEdit("Hello World",this);QSplitter splitter(Qt::Vertical);
  23.  splitter.addWidget(editor1);
  24.  layout->addWidget(editor1);
  25.         setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,255,255); padding: 7px}}");
  26.  
  27.  setLayout(layout);
  28.         showMaximized();
  29. }

You have several errors here, I post the messages from gcc (on a Mac). You are on Windows using Visual Studio, so the error messages are different, but should be worded similar.

projectisthisabadgame.cpp:18: Error:prototype for ‘splitter::splitter(QWidget*)’ does not match any in class ‘splitter’

Look at the declaration of the constructor in splitter.h line 16:

  1.     splitter(QWidget *parent = 0, Qt::WFlags flags = 0);

and compare with the signature of the constructor you actually implemented in projectisthisabadgame.cpp:

  1. splitter::splitter(QWidget *parent)
  2.     : QWidget(parent)
  3. {
  4.    // ...
  5. }

You see: you declared a constructor with two parameters, but implemented it with only one parameter. You can add the second parameter in the .cpp or remove the second one in the .h. Go with the first, as this is the same signature as the standard constructor of QDialog [doc.qt.nokia.com].

This ist the correct constructor signature:

  1. splitter::splitter(QWidget *parent, Qt::WFlags flags)
  2.     : QWidget(parent)
  3. {
  4.     // ...
  5. }

Another compiler run gives us the next error message:

projectisthisabadgame.cpp:19: Error:type ‘class QWidget’ is not a direct base of ‘splitter’

This is easy to fix, too. In splitter.h you declared class splitter to inherit from QDialog. In the implementation you call a QWidget constructor (which is a base class of QDialog; line 19 in projectisthisabadgame.cpp). But this is wrong, you must always call the constructors of the direct base class in your own constructors. So, change this to:

  1. splitter::splitter(QWidget *parent, Qt::WFlags flags)
  2.     : QDialog(parent, flags)
  3. {
  4.    // ...
  5. }

Also, notice the handing over of the second parameter (flags) to QDialog’s constructor.

Your code now compiles without errors. The next errors are popped up by the linker (which does not compile anything, but puts the previously compiled little pieces together to the final application).

Undefined symbols: splitter::~splitter() and splitter::showHide()

In your splitter.h you declared a destructor ~splitter() (line 17) and a method showHide() (line 20), but you did not provide an implementation in your .cpp file. Just add some dummy implementation in the .cpp:

  1. splitter::~splitter()
  2. {
  3. }
  4.  
  5. void splitter::showHide()
  6. {
  7. }

Please note that a parameter-less method takes no argument. It’s not an error to write showHide(void), but most programmers consider this as unnecessary chatter. Better omit it.

Your code now compiles and links without errors.

Just another tip: In your project you have splitter.h and projectisthisabadgame.h declaring the classes. You implement both in projectisthisabadgame.cpp. This works, no question, but it is not a good idea. Better to separate your classes’ implementations into separate files too (i.e. move the splitter stuff into a new file splitter.cpp)

Feel free to come back with further questions.

April 10, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Thank you! :D

April 12, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Sry, but I seem to get a, “cannot open file, ‘QTHelpd4.lib,’” error, even after compiling Qt from source…

Help please!

Thank you :D
M260

April 12, 2011

Volker Volker
Ant Farmer
5428 posts

Did you add

  1. CONFIG += help

to your .pro file?

April 17, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Thanks, I’ll try it!

EDIT: Well, you see, I’m pretty sure Visual Studio uses plain ol’ cpp files instead of pro files, just like normal C++. So, I added it to the end of projectisthisabadgame.cpp & got this output:

  1. 1>------ Build started: Project: Project Is This a Bad Game, Configuration: Debug Win32 ------
  2. 1>Build started 4/17/2011 10:32:12 AM.
  3. 1>InitializeBuildStatus:
  4. 1>  Touching "Debug\Project Is This a Bad Game.unsuccessfulbuild".
  5. 1>CustomBuild:
  6. 1>  All outputs are up-to-date.
  7. 1>ClCompile:
  8. 1>  projectisthisabadgame.cpp
  9. 1>projectisthisabadgame.cpp(39): error C2143: syntax error : missing ';' before '+='
  10. 1>projectisthisabadgame.cpp(39): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  11. 1>
  12. 1>Build FAILED.
  13. 1>
  14. 1>Time Elapsed 00:00:03.26
  15. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Can anyone help?

Thanks,
M260

April 17, 2011

Azr79 Azr79
Lab Rat
33 posts

Show us your projectisthisabadgame.cpp please, in particulary from line 37 – 40

 Signature 

Azr79

April 18, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Well, actually, an old version is shown earlier in this thread, but here’s an updated version:

  1. #include "projectisthisabadgame.h"
  2. #include "splitter.h"
  3. #include<QSplitter>
  4. #include<QTextEdit>
  5. #include<QHBoxLayout>
  6.  
  7. ProjectIsThisaBadGame::ProjectIsThisaBadGame(QWidget *parent, Qt::WFlags flags)
  8.  : QMainWindow(parent, flags)
  9. {
  10.  ui.setupUi(this);
  11. }
  12.  
  13. ProjectIsThisaBadGame::~ProjectIsThisaBadGame()
  14. {
  15.  
  16. }
  17.  
  18. splitter::splitter(QWidget *parent, Qt::WFlags flags)
  19.     : QDialog(parent, flags)
  20. {
  21.  QHBoxLayout *layout=new QHBoxLayout(this);
  22.  QTextEdit *editor1 = new QTextEdit("Hello World",this);QSplitter splitter(Qt::Vertical);
  23.  splitter.addWidget(editor1);
  24.  layout->addWidget(editor1);
  25.         setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,255,255); padding: 7px}}");
  26.  
  27.  setLayout(layout);
  28.         showMaximized();
  29. }
  30.  
  31. splitter::~splitter()
  32. {
  33. }
  34.  
  35. void splitter::showHide()
  36. {
  37. }
  38.  
  39. CONFIG += help

EDIT: Um, the forum messed up the formatting a bit I think, but, whatever…

April 18, 2011

Azr79 Azr79
Lab Rat
33 posts

Line 39!!!!

  1. CONFIG += help

got to be removed from this file and add it into your .pro file, try to recompile then.

 Signature 

Azr79

April 24, 2011

MASTER260 MASTER260
Lab Rat
14 posts

But the reason I added it there is because it seems Visual Studio doesn’t support pro files…

April 24, 2011

Azr79 Azr79
Lab Rat
33 posts

I think that you got to generate it with qmake.exe and add it in your links, i’ll search more infos for later, stand by mate

 Signature 

Azr79

April 26, 2011

Volker Volker
Ant Farmer
5428 posts

You can use the Visual Studio Add-in [qt.nokia.com] to manage the mapping between .pro files and Visual Studio project files. The manual is here [doc.qt.nokia.com].

April 26, 2011

Azr79 Azr79
Lab Rat
33 posts

OK, here is the deal:
You have to generate a .pro file with ‘Qt command prompt’ (look into qt root folder)
Start Qt Command prompt and enter following commands:

-> cd c:\Folder\of\your\project\ (directory that contains Visual studio’s project file)
-> qmake -project
-> qmake

after these commands look into your project folder, you’ll see some auto generated files and folders, and .pro file will be there, just edit it however you want.

If you want to compile just continue with the following command:
-> make

 Signature 

Azr79

April 27, 2011

MASTER260 MASTER260
Lab Rat
14 posts

Thanks, but after doing qmake, this happened:

http://i55.tinypic.com/ivzpcp.png

April 27, 2011

Volker Volker
Ant Farmer
5428 posts

A couple of warnings – nothing to worry about. What happens after running nmake?

April 27, 2011

Azr79 Azr79
Lab Rat
33 posts

Is your project compiled?

By the way, use preferably directories without spaces for your projects, very important!
Ex.: c:\Dev\Qt_Projects\This_is_a_very_bad_game\
Never use spaces, compilator might show you some errors.

 Signature 

Azr79

Page  
1

  ‹‹ [Solved] Remove System menu bar mac os x      Make a label editable after clicking on it. How? ››

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