Results 1 to 9 of 9

Thread: Weird problem with translate()

  1. #1
    Join Date
    Jul 2011
    Location
    Brazil
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Weird problem with translate()

    Hello.

    OK, here's the problem: I have this class:

    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. #include <QList>
    4. #include <QModelIndex>
    5.  
    6. namespace Ui {
    7. class StimulatorMainWindow;
    8. }
    9. class Backend;
    10. class ChannelsTabWidget;
    11. class ConfigurationDialog;
    12. class CustomTreatmentDialog;
    13. class PatternsListDelegate;
    14. class TreatmentsModel;
    15.  
    16. class QLabel;
    17. class QMenu;
    18. class QStateMachine;
    19. class QStyledItemDelegate;
    20.  
    21. class StimulatorMainWindow : public QMainWindow
    22. {
    23. Q_OBJECT
    24.  
    25. public:
    26. explicit StimulatorMainWindow(QWidget *parent = 0);
    27. ~StimulatorMainWindow();
    28.  
    29. protected:
    30. void closeEvent(QCloseEvent *);
    31.  
    32. const QVariantHash &currentIndexData();
    33.  
    34. private:
    35. PatternsListDelegate *_patternsListDelegate;
    36. QStyledItemDelegate *_regularItemDelegate;
    37. CustomTreatmentDialog *_treatmentEditDialog, *_treatmentCreateDialog;
    38. QModelIndex _currentPatternIndex;
    39. QMessageBox *_treatmentWarning;
    40. ConfigurationDialog *_configDiag;
    41. QMenu *_patternsMenu;
    42. QAction *_newTreatmentSeparator;
    43.  
    44. QStateMachine *_interfaceMachine;
    45.  
    46. Backend *_backend;
    47. TreatmentsModel *_patternsModel;
    48.  
    49. Ui::StimulatorMainWindow *_ui;
    50.  
    51. /* Methods and signals */
    52. };
    To copy to clipboard, switch view to plain text mode 

    But, if I try to declare a new variable inside it, say, an int, I will definitively get a segfault if I use that variable.

    If I declare this variable before Ui::StimulatorMainWindow *_ui;, I'll get a segfault right after trying to run the application, even if I don't use the variable. Here are Valgrind (http://pastebin.com/uAa8yaYq) and backtrace (http://pastebin.com/1jQF8X4u) for this case.

    But if I declare the new variable after Ui::StimulatorMainWindow *_ui; and then use it in the class definition (by, for example, attributing it some value), I'll get a segfault from the first tr() after its usage, but not from the ones before. Valgrind (http://pastebin.com/58DrJ3A9) and backtrace (http://pastebin.com/1jQF8X4u).

    Anyway, this is related to the translate() method, but I have absolutely no idea how. It never happened before and, as I said, all I did was to declare a new variable and attribute it a value.

    What's going on? What's wrong with my class?

    I'm completely lost on this one, so any kind of help would be appreciated.

    Thanks.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Weird problem with translate()

    You need to post some of your implementation (ie, .cpp file). Maybe you are trying to use some part of the class after the 'this' pointer becomes invalid.

  3. #3
    Join Date
    Jul 2011
    Location
    Brazil
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird problem with translate()

    So, let's say I declared an int _aaa in line 52 on the snippet above. In that case, as I said, I don't even need to do anything to the cpp file to get a segfault.

    But let's suppose I declared int _aaa in line 54, after Ui::StimulatorMainWindow *_ui, and use it like this:
    Qt Code:
    1. StimulatorMainWindow::StimulatorMainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. _patternsListDelegate(new PatternsListDelegate(this)),
    4. _regularItemDelegate(new QStyledItemDelegate(this)),
    5. _treatmentEditDialog(new CustomTreatmentDialog(this)),
    6. _treatmentCreateDialog(new CustomTreatmentDialog(this)),
    7. _configDiag(new ConfigurationDialog(this)),
    8. _patternsMenu(new QMenu(this)),
    9. _interfaceMachine(new QStateMachine(this)),
    10. _patternsModel(new TreatmentsModel(this)),
    11. _ui(new Ui::StimulatorMainWindow)
    12. {
    13. setupUi();
    14. loadConfigs();
    15. initBackend();
    16. setupStates();
    17. setupModels();
    18. setupActions();
    19. setupConnections();
    20. initWaveTypes();
    21. }
    22.  
    23. void StimulatorMainWindow::setupUi()
    24. {
    25. _ui->setupUi(this);
    26.  
    27. _ui->patternWidget->hide();
    28. _ui->engineDetailsWidget->hide();
    29.  
    30. _ui->centralWidgetLayout->addWidget(_treatmentEditDialog);
    31. _ui->deleteConfirmationButtons->addButton(tr("Yes"), QDialogButtonBox::YesRole);
    32. _ui->deleteConfirmationButtons->addButton(tr("No"), QDialogButtonBox::NoRole);
    33.  
    34. _ui->treatmentPatternsList->setItemDelegate(_patternsListDelegate);
    35.  
    36. _treatmentWarning = new QMessageBox;
    37. _treatmentWarning->setText(tr("Please check ALL connectors before proceeding."));
    38. _treatmentWarning->setInformativeText(tr("Starting treatment without attaching <u>all</u> connectors correctly may cause <b>severe injuries</b> to the patient."));
    39. QPushButton *cancel = _treatmentWarning->addButton(tr("Cancel"), QMessageBox::RejectRole);
    40. _treatmentWarning->addButton(tr("Proceed"), QMessageBox::YesRole);
    41. _treatmentWarning->setDefaultButton(cancel);
    42. _treatmentWarning->setIcon(QMessageBox::Warning);
    43. _treatmentWarning->setWindowTitle(tr("Check all connectors first"));
    44.  
    45. _aaa = 12; // The only place I use _aaa. All those tr()s above work normally.
    46.  
    47. statusBar()->hide();
    48. }
    49.  
    50. void StimulatorMainWindow::initBackend()
    51. {
    52. QString validBackendNeeded =
    53. tr("A valid backend executable is needed for the stimulations to work, but none is set. Please choose one at %1"
    54. ).arg("Configure... > Backend"), // But then I get a segfault right here, because this method comes after my setupUi()
    55. noFeatureSupported =
    56. tr("No feature is supported by the current backend. %1",
    57. "The argument is the rest of the message"
    58. ).arg(tr("Please choose another backend for full feature support at %1"
    59. ).arg(tr("Configure... > Backend"))),
    60. someFeaturesMissing =
    61. tr("Some features are not supported by the current backend: %1",
    62. "The argument is the rest of the message"
    63. ).arg(tr("Please choose another backend for full feature support at %1"
    64. ).arg(tr("Configure... > Backend")));
    65.  
    66. _aaa = 44; // If _aaa was initialized here instead of in the method above, any subsequent tr() calls would segfault as well
    67.  
    68. // Rest of the method
    69. }
    70.  
    71. // Rest of the class
    To copy to clipboard, switch view to plain text mode 

    I hope that makes my point clear.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Weird problem with translate()

    This might be total unrelated observation - Storing QModelIndex is not a good idea, check the note from documentation, also hope you are not using QModelIndex _currentPatternIndex; (or any other pointer) before setting it up.

    Quote Originally Posted by Qt 4.7 Documentation
    Note: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex.

  5. #5
    Join Date
    Jul 2011
    Location
    Brazil
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird problem with translate()

    Quote Originally Posted by Santosh Reddy View Post
    This might be total unrelated observation - Storing QModelIndex is not a good idea, check the note from documentation,
    QPersistentModelIndex was giving me some weird bugs that vanished when I started to use QModelIndex.

    Quote Originally Posted by Santosh Reddy View Post
    also hope you are not using QModelIndex _currentPatternIndex; (or any other pointer) before setting it up.
    I'm pretty sure I'm not.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Weird problem with translate()

    the second backtrace posted is same as first one

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Weird problem with translate()

    1. title of topic is strange, it is almost not related to your problem. I would say proper title should be: "Class crushes when any new field is added"
    2. this kind of problems usually are result of errors in other parts of code. Problem just manifest it self in this place. There can be couple reasons: dangling pointer, wrong type cast, binary compability break (when using dlls), or some compiler cached data not updated correctly (here point 4 will help).
    3. I'm afraid that finding problem without having whole project will be quite hard
    4. did you clean project (call "make clean" or rebuild project), sometimes it helps in such cases.
    Last edited by MarekR22; 20th July 2011 at 10:35.

  8. #8
    Join Date
    Jul 2011
    Location
    Brazil
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird problem with translate()

    Quote Originally Posted by MarekR22 View Post
    1. title of topic is strange, it is almost not related to your problem. I would say proper title should be: "Class crushes when any new field is added"
    2. this kind of problems usually are result of errors in other parts of code. Problem just manifest it self in this place. Problem just manifest it self in this place. There can be couple reasons: dangling pointer, wrong type cast, binary compability break (when using dlls), or some compiler cached data not updated correctly (here point 4 will help).
    3. I'm afraid that finding problem without having whole project will be quite hard
    4. did you clean project (call "make clean" or rebuild project), sometimes it helps in such cases.
    1 - Noted, sorry about that. It is related to translate(), though.
    2 - Well, it never happened before and how can it be in another part of the code if I don't even use the new field?
    Still in 2, about the reasons:
    - Dangling pointer: You mean, the newly added one? If that's so, it happens with any type, even with an int, so I don't know how that can be the problem.
    - Wrong type cast: Again, if you mean the new field, it happens with any type.
    - Binary compability break (when using dlls): I was on Windows before, but it also happens on Linux.
    - Compiler cached data: I don't believe so and I'll tell you why in 4.
    3 - As I am, as well
    4 - It was one of the first things I tried, but it didn't help at all.

    Quote Originally Posted by Santosh Reddy View Post
    the second backtrace posted is same as first one
    D'oh!

    Here they are:
    When I create the new field before _ui: http://pastebin.com/1jQF8X4u
    When I create the new field after _ui and then use it somewhere in the code: http://pastebin.com/hJrG5CFf

  9. #9
    Join Date
    Jul 2011
    Location
    Brazil
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird problem with translate()

    Well, just reporting again to say that the cause of the problem.

    Some time ago, I changed the name of mainwindow.hpp to stimulatormainwindow.hpp, but I forgot to change the name of the #include in main.cpp and - I don't know why - to remove the old mainwindow.hpp. So, while I didn't change StimulatorMainWindow, it was fine, because the old mainwindow.hpp still remained valid.

    But when I needed to change it for real - that is, now - the problem blew up in my face.

    I feel dumb.

    Anyway, thanks to everyone that tried to help.

Similar Threads

  1. Problem with translate one element
    By Hostel in forum Newbie
    Replies: 0
    Last Post: 19th November 2010, 00:00
  2. Weird text input problem
    By zeldaknight in forum Qt Programming
    Replies: 6
    Last Post: 10th July 2010, 21:19
  3. Weird problem with QTableView::setResizeMode
    By winkle99 in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2010, 04:16
  4. weird problem in QGraphicsView / QGraphicsScene
    By Mrdata in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2009, 19:26
  5. Weird problem while porting from Qt3 to Qt4
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 8th August 2007, 07:51

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.