Page 1 of 1

[SOLVED] Qt4 Custom Window not compiling

Posted: Fri Jan 15, 2010 4:24 pm
by lotios611
I'm trying to make a custom QMainWindow. I have this error:
:20: multiple definition of `non-virtual thunk to MainWindow::~MainWindow()'
:15: multiple definition of `MainWindow::~MainWindow()'
:9: first defined here
Here's the code I'm trying to compile:

MainWindow.h:

Code: Select all

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QFileDialog>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QMainWindow *parent = 0);
    virtual ~MainWindow();

private:
    QMenu *file;
    QAction *save;
    QFileDialog *saveDialog;

private slots:
    void newLevel();
    void saveLevel();
    void loadLevel();
};

#endif // MAINWINDOW_H
Here's MainWindow.cpp:

Code: Select all

#include "MainWindow.h"

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
    setWindowIcon(QIcon("icon.png"));
    setWindowTitle("Level Editor Development Version");
    setPalette(QPalette(Qt::white));

    file = menuBar()->addMenu("&File");
    save = new QAction("&Save", this);
    file->addAction(save);
    connect(save, SIGNAL(triggered()), this, SLOT(saveLevel()));
}

MainWindow::~MainWindow()
{
    delete file;
    delete save;
    delete saveDialog;
}

void MainWindow::saveLevel()
{
    saveDialog->getSaveFileName(NULL, "File:");
    saveDialog->show();
}

void MainWindow::loadLevel()
{
}

void MainWindow::newLevel()
{
}
I'm using Qt Creator to develop.

Re: Qt4 Custom Window not compiling

Posted: Sat Jan 16, 2010 6:19 am
by dani93
Works fine for me on Ubuntu with Qt 4.5.2. However there are two bugs in it:
1) You don't allocate memory for saveDialog -> Segmentation fault
2) You don't need to call getSaveFileName and show on saveDialog. getSaveFileName alone is enough.
Try recompiling the complete project (also run qmake), this often removes weird errors.

Re: Qt4 Help

Posted: Sat Jan 16, 2010 7:07 am
by lotios611
Thanks! I have another question though. I'm trying to open a program from within my program. I've looked into QProcess, but none of the examples I've tried work.

Re: Qt4 Help

Posted: Sat Jan 16, 2010 9:24 am
by dani93
lotios611 wrote:Thanks!
So you solved the problem by just recompiling the project?
lotios611 wrote:I have another question though. I'm trying to open a program from within my program. I've looked into QProcess, but none of the examples I've tried work.
I have never used QProcess, so I can't tell you much about it. Maybe if you show me your code. "system" would be a very ugly but simple solution.

Re: Qt4 Custom Window not compiling

Posted: Sat Jan 16, 2010 9:36 am
by lotios611
dani93 wrote:
lotios611 wrote:Thanks!
So you solved the problem by just recompiling the project?
That, and changing the other things you said.
dani93 wrote: I have never used QProcess, so I can't tell you much about it. Maybe if you show me your code. "system" would be a very ugly but simple solution.
Here's some code:

Code: Select all

#include <QtGui>
#include <QProcess>
#include <QStringList>
#include "MainWindow.h"

 int main(int argc, char *argv[])
 {
    QApplication app(argc, argv);
    MainWindow window; //My Custom window
    window.show();
    QProcess process(&window);
    process.start("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    return app.exec();
 }

Re: Qt4 Custom Window not compiling

Posted: Sat Jan 16, 2010 9:57 am
by dani93
lotios611 wrote:Here's some code:
[...]
Works perfect for me again. Another reason I don't like Windows... the paths. Although you already corrected the backslashes in the path, I'm quite sure that the spaces are making problems. Don't know how you can avoid such things on Windows, but you could try something like that:

Code: Select all

process.start("\"C:\\Program Files\\Mozilla Firefox\\firefox.exe\"");
So Windows will interpret it as one command, not as several separated by spaces (I hope...).

BTW: If you only want to open an URL in the default browser you can use QDesktopServices::openUrl (QUrl); You can never be sure that everyone has installed Firefox (even in the same directory?!). Qt was designed to be platform independent. Something like starting a process with a static path destroys everything.

Re: Qt4 Custom Window not compiling

Posted: Sat Jan 16, 2010 10:14 am
by lotios611
Thanks again. I was using firefox to test it out. BTW, how long have you been using Qt? I just started, I think that my project's way too ambitious.

Re: Qt4 Custom Window not compiling

Posted: Sat Jan 16, 2010 10:57 am
by dani93
lotios611 wrote:BTW, how long have you been using Qt? I just started, I think that my project's way too ambitious.
I have been using Qt for about half a year. I think it is incredibly easy to learn.