PDA

View Full Version : Having some QObject problems



Barvik
12th November 2008, 18:01
I get the following error when i try to run my program, it compiles with no errors.


QObject::setParent: New parent must be in the same thread as the previous parent
Segmentation fault.

Been trying to sort it for hours now, and i'm out of ideas.

here's my headerfile:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDate>
#include <QMainWindow>
#include <QTableWidget>


class QAction;
class QLabel;

class MainWindow : public QTableWidget
{
Q_OBJECT

public:
MainWindow();


public slots:
void newFile();
/*void open();
bool save();
bool saveAs();
void find();
void goToCell();
void sort();
void about();
void openRecentFile();
void updateStatusBar();*/



private:
void lagKalender();
void createActions();
void createMenus();
void createContextMenu();
void createToolBars();
void readSettings();
void writeSettings();
bool okToContinue();
bool loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
void setCurrentFiles(const QString &fileName);
void updateRecentFileAtions();
QString strippedName(const QString &fullFileName);

QTableWidget *item1;
QLabel *locationLabel;
QLabel *formulaLabel;
QStringList recentFiles;
QString curFile;

enum { MaxRecentFiles = 5 };
QAction *recentFileActions[MaxRecentFiles];
QAction *separatorAction;

QMenuBar *menuBar;

QMenu *fileMenu;
QMenu *editMenu;
QMenu *selectSubMenu;
QMenu *toolsMenu;
QMenu *optionsMenu;
QMenu *helpMenu;

QToolBar *fileToolBar;
QToolBar *editToolBar;

QAction *newAction;
QAction *openAction;
QAction *exitAction;
QAction *saveAction;
QAction *saveAsAction;
QAction *cutAction;
QAction *copyAction;
QAction *pasteAction;
QAction *deleteAction;
QAction *aboutQtAction;
QAction *selectRowAction;
QAction *selectCollumnAction;
QAction *selectAllAction;
QAction *findAction;
QAction *goToCellAction;
QAction *recalculateAction;
QAction *sortAction;
QAction *showGridAction;
QAction *autoRecalcAction;
QAction *aboutAction;
QAction *setShortCut;


};
#endif


There are some stuff that havent been used yet in, but i cant do any other stuff untill i sort this problem.

main code file:


#include <QtGui>
#include "mainwindow.h"



MainWindow::MainWindow()
{
createActions();
createMenus();
lagKalender();






}
void MainWindow::lagKalender()
{




this->setRowCount(7);
this->setColumnCount(7);

this->setHorizontalHeaderItem(0, new QTableWidgetItem("Monday"));
this->setHorizontalHeaderItem(1, new QTableWidgetItem("Tuesday"));
this->setHorizontalHeaderItem(2, new QTableWidgetItem("Wednesday"));
this->setHorizontalHeaderItem(3, new QTableWidgetItem("Thursday"));
this->setHorizontalHeaderItem(4, new QTableWidgetItem("Friday"));
this->setHorizontalHeaderItem(5, new QTableWidgetItem("Saturday"));
this->setHorizontalHeaderItem(6, new QTableWidgetItem("Sunday"));

this->setVerticalHeaderItem(0, new QTableWidgetItem("Week1"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week2"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week3"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week4"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week5"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week6"));
this->setVerticalHeaderItem(0, new QTableWidgetItem("Week7"));

for (int i=0; i < 31; i++)
{
int column = i / 7;
int row = i % 7;
QString s;
s.setNum (i+1);
this->setItem(column, row, new QTableWidgetItem(s));
}
}
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/new.pgn"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new calendar"));
connect(newAction, SIGNAL(triggered()),
this, SLOT(newFile()));

exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()),
this, SLOT(close()));


aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus()
{

fileMenu = menuBar->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);

separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i){
fileMenu->addAction(recentFileActions[i]);
}
fileMenu->addSeparator();
fileMenu->addAction(exitAction);

editMenu = menuBar->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);

selectSubMenu = editMenu->addMenu(tr("&Select"));
selectSubMenu->addAction(selectRowAction);
selectSubMenu->addAction(selectCollumnAction);
selectSubMenu->addAction(selectAllAction);

editMenu->addSeparator();
editMenu->addAction(findAction);
editMenu->addAction(goToCellAction);

toolsMenu = menuBar->addMenu(tr("&Tools"));
toolsMenu->addAction(recalculateAction);
toolsMenu->addAction(sortAction);

optionsMenu = menuBar->addMenu(tr("&Options"));
optionsMenu->addAction(showGridAction);
optionsMenu->addAction(autoRecalcAction);

menuBar->addSeparator();

helpMenu = menuBar->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
helpMenu->addAction(aboutQtAction);
}
void MainWindow::newFile()
{

if (okToContinue()) {

}
}

bool MainWindow::okToContinue()
{
if(isWindowModified()) {
int r = QMessageBox::warning(this, tr("Kalender"),
tr("The documenet has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if(r == QMessageBox::Yes){
return true;
}
else if(r == QMessageBox::Cancel){
return false;
}
}
return true;
}

and the main:

#include <QtGui>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.resize(640, 256);
window.show();
return app.exec();
}

Thxn in advance for helping

spirit
12th November 2008, 18:08
looks good. did you try to rebuild project?

Barvik
12th November 2008, 18:22
jea, tried to rebuild porject, but i'm stil getting the Segmentation fault.

spirit
12th November 2008, 18:25
did you remove all object and executable file and then rebuild project?
as I see there no another thread in your code, isn't it?

Barvik
12th November 2008, 18:34
nope, i still get the same errors. But i can now tell from the code that there is something wrong with

QMenuBar *menuBar; in the headerfile.

Cause if i comment out the createMenus() there is no segmentation error.

could it be that i'm enheriting from the QTableWidget class and are trying to use the QMainWindow class?

cause i'm trying to use the menuBar() method provided in QMainWindow

spirit
12th November 2008, 18:58
oh, I didn't notice that


class MainWindow : public QTableWidget

if want to use QToolBar and other features which provide QMainWindow then you must derive your class from QMainWindow, i.e.


class MainWindow : public QMainWindow

Barvik
12th November 2008, 22:33
Thxn for the help so far, the segmentation fault is now gone. But now i cant use the QTableWidget class properbly. U have any ideas how i can sort out he lagKalender() method?

aamer4yu
13th November 2008, 04:29
make a separate class for table widget, and set it as central widget of main window