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
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 —-
- #ifndef SPLITTER_H
- #define SPLITTER_H
- #include <QtGui/QDialog>
- {
- Q_OBJECT;
- public:
- ~splitter();
- private slots:
- void showHide(void);
- };
- #endif // SPLITTER_H
—- projectisthisabadgame.cpp —-
- #include "projectisthisabadgame.h"
- #include "splitter.h"
- #include<QSplitter>
- #include<QTextEdit>
- #include<QHBoxLayout>
- {
- ui.setupUi(this);
- }
- ProjectIsThisaBadGame::~ProjectIsThisaBadGame()
- {
- }
- {
- splitter.addWidget(editor1);
- layout->addWidget(editor1);
- setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,255,255); padding: 7px}}");
- setLayout(layout);
- showMaximized();
- }
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:
and compare with the signature of the constructor you actually implemented in projectisthisabadgame.cpp:
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:
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:
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:
- splitter::~splitter()
- {
- }
- void splitter::showHide()
- {
- }
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.
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>------ Build started: Project: Project Is This a Bad Game, Configuration: Debug Win32 ------
- 1>Build started 4/17/2011 10:32:12 AM.
- 1>InitializeBuildStatus:
- 1> Touching "Debug\Project Is This a Bad Game.unsuccessfulbuild".
- 1>CustomBuild:
- 1> All outputs are up-to-date.
- 1>ClCompile:
- 1> projectisthisabadgame.cpp
- 1>projectisthisabadgame.cpp(39): error C2143: syntax error : missing ';' before '+='
- 1>projectisthisabadgame.cpp(39): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- 1>
- 1>Build FAILED.
- 1>
- 1>Time Elapsed 00:00:03.26
- ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Can anyone help?
Thanks,
M260
Well, actually, an old version is shown earlier in this thread, but here’s an updated version:
- #include "projectisthisabadgame.h"
- #include "splitter.h"
- #include<QSplitter>
- #include<QTextEdit>
- #include<QHBoxLayout>
- {
- ui.setupUi(this);
- }
- ProjectIsThisaBadGame::~ProjectIsThisaBadGame()
- {
- }
- {
- splitter.addWidget(editor1);
- layout->addWidget(editor1);
- setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,255,255); padding: 7px}}");
- setLayout(layout);
- showMaximized();
- }
- splitter::~splitter()
- {
- }
- void splitter::showHide()
- {
- }
- CONFIG += help
EDIT: Um, the forum messed up the formatting a bit I think, but, whatever…
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].
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
You must log in to post a reply. Not a member yet? Register here!


