Click to See Complete Forum and Search --> : Qt Questions


Dun'kalis
01-25-2003, 11:25 PM
I'm learning Qt, and its really well-designed, but I really can't get this to work.

I want to open a file and load that in to the proper text widget.

The text widget is derived from QTextEdit, and is called TextEditor (very original). The widget class declaration is in editor.h, and the code that creates an instance of that class is in editor.cpp.

void EditorMenu::open()
{
QString fileName = QFileDialog::getOpenFileName(
"home",
"C/C++ Files (*.c, *.C, *.cpp, *.cxx, *.h)",
this,
"open file dialog",
"Open File...");

QFile fileData(fileName);
fileData.open;
editor->setText(fileData); // hmm...
}

EditorMenu is a QMenuBar instance. I want to load the file fileName to the TextEditor control. I'm guessing this is close to the right way, but when it compiles, it spits back an error.

In my editor.cpp, I have the following in it (there is more, but its not important)

QTextEdit *editor = new QTextEdit(this);

I also tried it with Editor->setText(fileData); but that didn't work, either.

Whats wrong? I know that this has got to be hard to solve without all the code, but there is 186 lines, and I doubt anyone wants to sift through all that to solve a problem with one part of the code.

Another quicker question:

I want to have a menu entry for exiting, and I believe I should be able to do the following:
QObject::connect(&EditorMenu::closeApp, SIGNAL(activated()), qApp, SLOT(quit()));

Which pops out this error message on compile:
menus.h: In member function `void EditorMenu::closeApp()':
menus.h:101: no matching function for call to `QObject::connect(void
(EditorMenu::*)(), const char[13], QApplication*&, const char[8])'
/opt/qt/include/qobject.h:116: candidates are: static bool
QObject::connect(const QObject*, const char*, const QObject*, const char*)
/opt/qt/include/qobject.h:227: bool QObject::connect(const
QObject*, const char*, const char*) const


Any ideas?

bwkaz
01-26-2003, 12:21 AM
Originally posted by Dun'kalis

fileData.open; Shouldn't this be an actual function call? Something like fileData.open()?

Posting the compile error would help, too, but I'm sure you knew this, right? ;)

QObject::connect(&EditorMenu::closeApp, SIGNAL(activated()), qApp, SLOT(quit())); Hmmm... Maybe get rid of that & before EditorMenu::closeApp? Actually, is closeApp a QButton, QAction, or something like that? It should be...

connect() takes a reference to the two objects, doesn't it?

Dun'kalis
01-26-2003, 02:15 AM
Well, I fixed that fileData.open();, but the compile error is the same (forgot to post it the first time):

menus.h:86: `editor' undeclared (first use this function)
menus.h: In member function `void EditorMenu::open()':
menus.h:85: no matching function for call to `QFile::open()'
/opt/qt/include/qfile.h:73: candidates are: virtual bool QFile::open(int)
/opt/qt/include/qfile.h:74: bool QFile::open(int, FILE*)
/opt/qt/include/qfile.h:75: bool QFile::open(int, int)
menus.h:86: `editor' undeclared (first use this function)

As for the connect(), when I tried it the way I thought it should be done (closeApp), but it spit out some errors on compile.

menus.h: In member function `void EditorMenu::closeApp()':
menus.h:102: no matching function for call to `QObject::connect(<unknown type>,
const char[13], QApplication*&, const char[8])'
/opt/qt/include/qobject.h:116: candidates are: static bool
QObject::connect(const QObject*, const char*, const QObject*, const char*)
/opt/qt/include/qobject.h:227: bool QObject::connect(const
QObject*, const char*, const char*) const

FYI, this is while compiling moc_menus.h.

If I add an ampersand before closeApp, I get this:

menus.h: In member function `void EditorMenu::closeApp()':
menus.h:102: ISO C++ forbids taking the address of an unqualified non-static
member function to form a pointer to member function. Say `&EditorMenu::
closeApp'
menus.h:102: no matching function for call to `QObject::connect(void
(EditorMenu::*)(), const char[13], QApplication*&, const char[8])'
/opt/qt/include/qobject.h:116: candidates are: static bool
QObject::connect(const QObject*, const char*, const QObject*, const char*)
/opt/qt/include/qobject.h:227: bool QObject::connect(const
QObject*, const char*, const char*) const
make: *** [moc_menus.o] Error 1


closeApp() is a public slot of EditorMenu, which is derived from QWidget.

EDIT: Removing the ampersand give me the same error as before.

I'm just learning Qt, so this is all really confusing to me. I'm gonna try making closeApp() a QAction, but that shouldn't be the problem...

bwkaz
01-26-2003, 10:51 AM
Here we go. On the open() error, the compiler is telling you that you need to pass an int to the open() function. Checking the Qt docs for QFile::open revealed this:

http://doc.trolltech.com/3.1/qfile.html#open

which gives the list of flags that open() takes. You probably want either IO_ReadOnly or IO_ReadWrite.

----

On the other problem, if closeApp is a slot of EditorMenu, then you can't connect it to anything. If it's actually a signal of EditorMenu, then you should do something like this:

connect(EditorMenu, SIGNAL(closeApp()), qApp, SLOT(quit())); You can't connect a slot to anything. If you have a slot that you want to fire another signal, then you can emitSignal (or something like that) in the handler for the slot. Or, you can just call the intended target routine directly, if you would use the signal/slot for only a point-to-point communication (in that case, the signal/slot overhead is too much, so you'd want to skip it altogether).

Dun'kalis
01-26-2003, 12:26 PM
Thank you! I got the quit to work!

Now, as for the other problem, I still can't seem to get it to compile. I added IO_ReadOnly to the open(), but the other part doesn't compile (the one that sends the read text to the texteditor window). It tells me that 'editor' is undeclared, and if I use the class name instead (TextEditor), it spits out "parse error before ->".

bwkaz
01-26-2003, 02:52 PM
OK, but the error on the open() function is gone, right?

I'm not sure on this, without seeing the rest of the code, but I have a feeling there's a major design problem here. :eek:

You say that your TextEditor widget class is derived from QTextEdit, right? Then why is there a QTextEdit pointer in it? You can access QTextEdit's functionality through the inheritance if you have to.

OTOH, the function that you're writing isn't part of the TextEditor class. So it doesn't have access to anything declared in that class, unless it has a reference (or pointer) to a TextEditor instance...

I think it's time to post the rest of the code... just zipping & attaching it would be better than including all of it, though.

Dun'kalis
01-26-2003, 04:22 PM
I knew something like this would happen...I'm just learning, and I haven't spent much time on it, so I'm just gonna scrap it.

Yes, I did make some major mistakes. Imagine my frustration when I figured out that I couldn't put all the components together the way I designed it!

I call this learning how to program C++ and Qt at the same time (I knew a bit of C++, but now, I really know a lot).

I should probably put the program together quickly in Python, then translate that to C++, anyway.