[solved] 0xC0000005 - try to use button (MSV 2010, Qt 4.8.2, W7)



  • I created window in Qt Designer
    !http://chrn.wz.cz/QTerr.png(Qt Designer - picture)!
    Everything works fine (I can compile it and it seems Ok) but when I do any action with button during compilation is occur this error:

    bq. Unhandled exception at 0x66a1d4c1 (QtCored4.dll) in Database_normalizator.exe: 0xC0000005: Access violation reading location 0x00000025.

    my mainwindow.h:

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
    #include "ui_mainwindow.h"
    #include "AddAttDialog.h"

    class MainWindow : public QMainWindow, public Ui::MainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~MainWindow();

    public:
    Ui::MainWindow ui;

    public slots:
    void addAttribute();
    };

    #endif // MAINWINDOW_H@

    my mainwindow.cpp:

    @#include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);

    (*addAttPushButton).setText("hhh"); //!!!error!!!
    (*editAttPushButton).hide(); //!!!error!!!
    connect(addAttPushButton, SIGNAL(clicked()), //!!!error!!!
    this, SLOT(addAttribute()));
    }

    void MainWindow::addAttribute()
    {
    /AddAttDialog dialog(this);
    if (dialog.exec())
    QString str = dialog.lineEdit->text();
    /
    }

    MainWindow::~MainWindow()
    {

    }@

    If I don´t use "!!!error!!! lines application behaves correctly...



  • Hello.
    Did you try using -> here
    @(*addAttPushButton).setText("hhh"); //!!!error!!!@
    instead of dereferencing pointers?


  • Moderators

    @
    (*addAttPushButton).setText("hhh"); //!!!error!!!
    (*editAttPushButton).hide(); //!!!error!!!
    connect(addAttPushButton, SIGNAL(clicked()), //!!!error!!!
    this, SLOT(addAttribute()));
    @

    Are you sure that those pointer are pointing anywhere?
    It would not be compliant at least with older versions of designer.



  • You need something like this:
    @
    MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);

    ui.addAttPushButton->setText("hhh"); //!!!error!!!
    ui.editAttPushButton->hide(); //!!!error!!!
    connect(ui.addAttPushButton, SIGNAL(clicked()), //!!!error!!!
    this, SLOT(addAttribute()));
    }
    @
    PS: since addAttPushButton isn't declared in Mainwindow class, i guess that addAttPushButton is part of the ui object (you created it in the Designer)



  • Wilk: It was the same.

    koahnig: Yes, they do.

    Zlatomir: Thank you, you´re right it is working. I didn´t know that I have to use ui. prefix. In some project (which is not mine) it is without ui. and everything else is the same (or at least it seems to be to me).



  • ui is not a "prefix" it's an instance (in your case) or a pointer to an instance of a class that uic is generating from the xml file that you create using Qt Designer.
    You have that ui object in your class:
    @
    class MainWindow : public QMainWindow //***you don't need to derive from this second class if you use an instance - that ui object
    //, public Ui::MainWindow
    public:
    Ui::MainWindow ui;@

    Learn more about using designer file into C++ code "here":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html

    ***i wanted to ask how your code compiled, because it shouldn't, but now i saw - please read the documentation page i linked ;)

    //you can mark the topic as solved by edit the first post and add [solved] at the beginning of the title and also you might want to learn more about C++ (i might be wrong, but from that "prefix" i deduced you are not familiar with C++), two free books can be found with a search for Thinking in C++ - there are two very good C++ volumes written by Bruce Eckel - and are also available as free download.



  • It compiled Ok (actually now I have another error probably becouse of the same). I will look at your link - thank you.



  • It compiled ok because you had those member pointers both in your class and in the ui object (i didn't saw the second class you inherited until i copied the code to show you what is that ui), so you shouldn't use two methods to include the uic generated code, choose only one method.

    And the crash was happening because the member pointers (the ones that you tried to use) were uninitialized.



  • Now I finally read the link and it is clear to me where was the error. Nice to know it. :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.