PDA

View Full Version : C++/Qt5.9.1 - Showing a ui form, passed variables not accessible!



jimbo
27th November 2017, 20:40
Hello.

Qt 5.9.1

I'm showing a ui form and the passed variables come up as not accessible.
I had it working for a while and for some reason, now it does not.
I'm using some of Qt Creators built in slot mechanisms.
Maybe I deleted something, I don't know.
Anybody have an insiight?

Regards


//Debug output
Locals
activeLayer <not accessible> QString
btnArea 0x4 QWidget*
captionList <not accessible> QList<QString>
layout 0x30 QLayout*
parent 0x0 QWidget*
this @0x27d1c050 qg_divideoptions
Inspector
Expressions
Return Value
Tooltip

//Constructor
//activeLayer and buttonCaptions have valid data

QDialog *myNewform = new qg_divideoptions( activeLayer, buttonCaptions );

myNewform->setWindowFlags( myNewform->windowFlags() & ~Qt::WindowContextHelpButtonHint );
myNewform->setFixedSize(300, 400);

QObject::connect( myNewform,
SIGNAL( dataToSend(const int&, //newticks
const int&, //newsize
const bool&, //hidedhow
const bool&, //breaks
const int&)), //newLayer
this,
SLOT( gotData(const int, //ticksR
const int, //sizeR
const bool, //showhideR
const bool, //breaksR
const int)) ); //layerR

myNewform->exec(); //*** modal ***

//Form header
#ifndef QG_DIVIDEOPTIONS_H
#define QG_DIVIDEOPTIONS_H

#include <QDialog>
#include <QRadioButton>

namespace Ui {
class qg_divideoptions;
}

class qg_divideoptions : public QDialog
{
Q_OBJECT

public:
explicit qg_divideoptions( QString, QList<QString>, QWidget *parent = 0 );
~qg_divideoptions();
Ui::qg_divideoptions *ui;

private:
void closeEvent(QCloseEvent *event);
int newticks, newsize, newLayer, qty;
bool showhide, breaks;

signals:
void dataToSend( const int& newticks,
const int& newsize,
const bool& showhide,
const bool& breaks,
const int& newLayer );

private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
void on_Sp1_valueChanged( int );
void on_Sp2_valueChanged( int );
void on_showhide_pressed();
void on_breaks_pressed();
void slot_on_layer_btn_pressed();
void slot_btn_lost_focus( bool );
void delete_form_items();
};

#endif // QG_DIVIDEOPTIONS_H

//Form .cpp
qg_divideoptions::qg_divideoptions( QString activeLayer,
QList<QString> captionList,
QWidget *parent ) :
QDialog(parent),
ui(new Ui::qg_divideoptions)
{
this->show(); // for debug
qDebug() << "1" << captionList;
qDebug() << "2" << activeLayer;
qDebug() << "3" << ui->Sp1->value();
qDebug() << "4" << ui->Sp2->value();
newticks = ui->Sp1->value();
newsize = ui->Sp2->value();
qty = captionList.size();
showhide = false;
breaks = false;

QWidget* btnArea = new QWidget;
btnArea->setObjectName( "btnArea" );
btnArea->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
btnArea->setLayout(new QVBoxLayout(btnArea));
ui->scrollArea->setWidget(btnArea);

QLayout *layout = btnArea->layout();
layout->setSpacing(0);
for (int i = 0; i < qty; i++)
{
QRadioButton* btn = new QRadioButton( captionList.at(i) );

QObject::connect( btn, SIGNAL(toggled(bool)), this, SLOT(slot_btn_lost_focus(bool)) );
QObject::connect( btn, SIGNAL( clicked(bool) ), this, SLOT( slot_on_layer_btn_pressed() ) );

btn->setObjectName( QString::number(i) );
layout->addWidget( btn );
}
}

Ginsengelf
28th November 2017, 09:34
Hi, I am not sure if mixed declarations like in your signal and slot work (one uses references, the other does not).
Do you get a line about problems with the connection in the output window when you debug your program?

Ginsengelf

jimbo
28th November 2017, 10:01
Hello,

Thanks for your reply.

" I am not sure if mixed declarations like in your signal and slot work"

I can only say that at one point the program was working, and the connections
worked, that method was one I found on Google.
I wonder if the problem is with the auto slot mechanism in Qt Designer,
I see some notes on the intertnet about problems with this method,
some people say don't use it.
No debug warnings, the program crashes too soon.

Regards

d_stranz
28th November 2017, 21:29
I wonder if the problem is with the auto slot mechanism in Qt Designer

If you are writing explicit connect() statements in -your- code, then you aren't using Qt Designer's automatic connection mechanism. These come into play when you have a UI variable like "myButton" and you declare a slot in the class corresponding to the UI file like "on_myButton_clicked()". The MOC compiler will see this declaration, match it up to the corresponding UI variable, and make the connection for you.


No debug warnings, the program crashes too soon.

I suggest that the problem you are seeing has nothing to do with the UI file, connections, or anything else. If your program is crashing, then it is likely that you have either corrupted memory or the stack in some way and the crash is just the result. That the debugger can't access the values of your UI variables is a sure sign of this - the object you are trying to examine is corrupted or otherwise invalid, and the debugger can't resolve the values of the variables it contains.

Fix the problem that causes the crash first. Look for uninitialized or NULL variables, out-of-bounds writing to arrays, etc. And change the slot arguments to match the signal, as Ginsengelf says. One uses pass by value (int), the other pass by reference (int &).

Who cares if you found it on Google? Do you believe everything you read online? Let me tell you about my poor uncle who was assassinated in the coup and left me $25,000,000 but it is stuck in a bank back in his homeland. I could use your help getting it out, and I'll give you half of it as a reward...