Results 1 to 8 of 8

Thread: Having some QObject problems

  1. #1
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Having some QObject problems

    I get the following error when i try to run my program, it compiles with no errors.

    Qt Code:
    1. QObject::setParent: New parent must be in the same thread as the previous parent
    2. Segmentation fault.
    To copy to clipboard, switch view to plain text mode 

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

    here's my headerfile:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QDate>
    5. #include <QMainWindow>
    6. #include <QTableWidget>
    7.  
    8.  
    9. class QAction;
    10. class QLabel;
    11.  
    12. class MainWindow : public QTableWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow();
    18.  
    19.  
    20. public slots:
    21. void newFile();
    22. /*void open();
    23. bool save();
    24. bool saveAs();
    25. void find();
    26. void goToCell();
    27. void sort();
    28. void about();
    29. void openRecentFile();
    30. void updateStatusBar();*/
    31.  
    32.  
    33.  
    34. private:
    35. void lagKalender();
    36. void createActions();
    37. void createMenus();
    38. void createContextMenu();
    39. void createToolBars();
    40. void readSettings();
    41. void writeSettings();
    42. bool okToContinue();
    43. bool loadFile(const QString &fileName);
    44. bool saveFile(const QString &fileName);
    45. void setCurrentFiles(const QString &fileName);
    46. void updateRecentFileAtions();
    47. QString strippedName(const QString &fullFileName);
    48.  
    49. QTableWidget *item1;
    50. QLabel *locationLabel;
    51. QLabel *formulaLabel;
    52. QStringList recentFiles;
    53. QString curFile;
    54.  
    55. enum { MaxRecentFiles = 5 };
    56. QAction *recentFileActions[MaxRecentFiles];
    57. QAction *separatorAction;
    58.  
    59. QMenuBar *menuBar;
    60.  
    61. QMenu *fileMenu;
    62. QMenu *editMenu;
    63. QMenu *selectSubMenu;
    64. QMenu *toolsMenu;
    65. QMenu *optionsMenu;
    66. QMenu *helpMenu;
    67.  
    68. QToolBar *fileToolBar;
    69. QToolBar *editToolBar;
    70.  
    71. QAction *newAction;
    72. QAction *openAction;
    73. QAction *exitAction;
    74. QAction *saveAction;
    75. QAction *saveAsAction;
    76. QAction *cutAction;
    77. QAction *copyAction;
    78. QAction *pasteAction;
    79. QAction *deleteAction;
    80. QAction *aboutQtAction;
    81. QAction *selectRowAction;
    82. QAction *selectCollumnAction;
    83. QAction *selectAllAction;
    84. QAction *findAction;
    85. QAction *goToCellAction;
    86. QAction *recalculateAction;
    87. QAction *sortAction;
    88. QAction *showGridAction;
    89. QAction *autoRecalcAction;
    90. QAction *aboutAction;
    91. QAction *setShortCut;
    92.  
    93.  
    94. };
    95. #endif
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3.  
    4.  
    5.  
    6. MainWindow::MainWindow()
    7. {
    8. createActions();
    9. createMenus();
    10. lagKalender();
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17. }
    18. void MainWindow::lagKalender()
    19. {
    20.  
    21.  
    22.  
    23.  
    24. this->setRowCount(7);
    25. this->setColumnCount(7);
    26.  
    27. this->setHorizontalHeaderItem(0, new QTableWidgetItem("Monday"));
    28. this->setHorizontalHeaderItem(1, new QTableWidgetItem("Tuesday"));
    29. this->setHorizontalHeaderItem(2, new QTableWidgetItem("Wednesday"));
    30. this->setHorizontalHeaderItem(3, new QTableWidgetItem("Thursday"));
    31. this->setHorizontalHeaderItem(4, new QTableWidgetItem("Friday"));
    32. this->setHorizontalHeaderItem(5, new QTableWidgetItem("Saturday"));
    33. this->setHorizontalHeaderItem(6, new QTableWidgetItem("Sunday"));
    34.  
    35. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week1"));
    36. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week2"));
    37. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week3"));
    38. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week4"));
    39. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week5"));
    40. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week6"));
    41. this->setVerticalHeaderItem(0, new QTableWidgetItem("Week7"));
    42.  
    43. for (int i=0; i < 31; i++)
    44. {
    45. int column = i / 7;
    46. int row = i % 7;
    47. s.setNum (i+1);
    48. this->setItem(column, row, new QTableWidgetItem(s));
    49. }
    50. }
    51. void MainWindow::createActions()
    52. {
    53. newAction = new QAction(tr("&New"), this);
    54. newAction->setIcon(QIcon(":/images/new.pgn"));
    55. newAction->setShortcut(QKeySequence::New);
    56. newAction->setStatusTip(tr("Create a new calendar"));
    57. connect(newAction, SIGNAL(triggered()),
    58. this, SLOT(newFile()));
    59.  
    60. exitAction = new QAction(tr("E&xit"), this);
    61. exitAction->setShortcut(tr("Ctrl+Q"));
    62. exitAction->setStatusTip(tr("Exit the application"));
    63. connect(exitAction, SIGNAL(triggered()),
    64. this, SLOT(close()));
    65.  
    66.  
    67. aboutQtAction = new QAction(tr("About &Qt"), this);
    68. aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
    69. connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    70. }
    71. void MainWindow::createMenus()
    72. {
    73.  
    74. fileMenu = menuBar->addMenu(tr("&File"));
    75. fileMenu->addAction(newAction);
    76. fileMenu->addAction(openAction);
    77. fileMenu->addAction(saveAction);
    78. fileMenu->addAction(saveAsAction);
    79.  
    80. separatorAction = fileMenu->addSeparator();
    81. for (int i = 0; i < MaxRecentFiles; ++i){
    82. fileMenu->addAction(recentFileActions[i]);
    83. }
    84. fileMenu->addSeparator();
    85. fileMenu->addAction(exitAction);
    86.  
    87. editMenu = menuBar->addMenu(tr("&Edit"));
    88. editMenu->addAction(cutAction);
    89. editMenu->addAction(copyAction);
    90. editMenu->addAction(pasteAction);
    91. editMenu->addAction(deleteAction);
    92.  
    93. selectSubMenu = editMenu->addMenu(tr("&Select"));
    94. selectSubMenu->addAction(selectRowAction);
    95. selectSubMenu->addAction(selectCollumnAction);
    96. selectSubMenu->addAction(selectAllAction);
    97.  
    98. editMenu->addSeparator();
    99. editMenu->addAction(findAction);
    100. editMenu->addAction(goToCellAction);
    101.  
    102. toolsMenu = menuBar->addMenu(tr("&Tools"));
    103. toolsMenu->addAction(recalculateAction);
    104. toolsMenu->addAction(sortAction);
    105.  
    106. optionsMenu = menuBar->addMenu(tr("&Options"));
    107. optionsMenu->addAction(showGridAction);
    108. optionsMenu->addAction(autoRecalcAction);
    109.  
    110. menuBar->addSeparator();
    111.  
    112. helpMenu = menuBar->addMenu(tr("&Help"));
    113. helpMenu->addAction(aboutAction);
    114. helpMenu->addAction(aboutQtAction);
    115. }
    116. void MainWindow::newFile()
    117. {
    118.  
    119. if (okToContinue()) {
    120.  
    121. }
    122. }
    123.  
    124. bool MainWindow::okToContinue()
    125. {
    126. if(isWindowModified()) {
    127. int r = QMessageBox::warning(this, tr("Kalender"),
    128. tr("The documenet has been modified.\n"
    129. "Do you want to save your changes?"),
    130. QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
    131. if(r == QMessageBox::Yes){
    132. return true;
    133. }
    134. else if(r == QMessageBox::Cancel){
    135. return false;
    136. }
    137. }
    138. return true;
    139. }
    To copy to clipboard, switch view to plain text mode 

    and the main:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MainWindow window;
    9. window.resize(640, 256);
    10. window.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Thxn in advance for helping

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Having some QObject problems

    looks good. did you try to rebuild project?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Having some QObject problems

    jea, tried to rebuild porject, but i'm stil getting the Segmentation fault.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Having some QObject problems

    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?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Having some QObject problems

    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

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Having some QObject problems

    oh, I didn't notice that
    Qt Code:
    1. class MainWindow : public QTableWidget
    To copy to clipboard, switch view to plain text mode 
    if want to use QToolBar and other features which provide QMainWindow then you must derive your class from QMainWindow, i.e.
    Qt Code:
    1. class MainWindow : public QMainWindow
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Nov 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Having some QObject problems

    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?

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having some QObject problems

    make a separate class for table widget, and set it as central widget of main window

Similar Threads

  1. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  2. Weird problems when destructor is called
    By cyberboy in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2008, 13:59
  3. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  4. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21
  5. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.