PDA

View Full Version : Processing multiple checkboxes



-Kyr0s-
14th October 2011, 14:56
Hello!

I'm a student and currently am working at a small company as an intern. I have to control some LEDs through usage of Qt, and made an interface to be able to do so. The interface issue concerned contains four checkboxes, and a button. It's supposed to send bytes to the MOXA board after having pressed the Send Data button.

My function checks whether the checkbox is enabled, and if so, send the bytes. However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).

I've done this, but this doesn't seem to work (enabling multiple checkboxes at once, that is):



void ETClient::on_pushSendData_clicked()
{
bool dio0 = this->ui->chkDIO0->isChecked();
bool dio1 = this->ui->chkDIO1->isChecked();
bool dio2 = this->ui->chkDIO2->isChecked();
bool dio3 = this->ui->chkDIO3->isChecked();

if(dio0 == true) this->dio0_enabled();
if(dio1 == true) this->dio1_enabled();
if(dio2 == true) this->dio2_enabled();
if(dio3 == true) this->dio3_enabled();
else ;
}


Any help would be greatly appreciated!

positive_4_life
14th October 2011, 16:11
However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).
Could you clarify this point? When you debug this with all checkboxes checked, what are your bool values?

-Kyr0s-
17th October 2011, 08:30
Hello,

Thanks for the swift reply – sorry for not replying any sooner than I have right now.

I've put breakpoints on the entire code that I've provided above.

Oddly enough, the bool values (when it processes the first definition of isChecked() - line 3 as seen above) are:

-dio0: 128
-dio1: 188
-dio2: 106
-dio3: false

When I continue to the next definition (line 4), the values are:

-dio0: false
-dio1: same
-dio2: same
-dio3: same

Then: false, false, same same -> false, false, false, same -> false, false, false, false.

Any suggestions? I can't seem to see why it does that.

Here's the entire code of the file, in-case needed:



#include "etclient.h"
#include "ui_etclient.h"
#include "about.h"

ETClient::ETClient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ETClient)
{
this->ui->setupUi(this);

this->connect(ui->menuAbout,SIGNAL(triggered()),this,SLOT(show_about ()));

this->connect(ui->pushConnect,SIGNAL(clicked()),this,SLOT(pre_begin( )));
this->connect(ui->pushDisconnect,SIGNAL(clicked()),this,SLOT(close_s ession()));
this->connect(&client,SIGNAL(error(QAbstractSocket::SocketError)) ,this,SLOT(connection_invalid()));
this->connect(&client,SIGNAL(connected()),this,SLOT(start_transfe r()));

this->ui->pushDisconnect->setShown(false);
}

ETClient::~ETClient()
{
client.close();
delete(this->ui);
}

void ETClient::show_about()
{
About *about = new About();
about->show();
}

void ETClient::pre_begin()
{
QString device_addr = this->ui->txtbxIP->text();
int device_port = this->ui->txtbxPort->text().toInt();

this->begin(device_addr,device_port);
}

void ETClient::begin(QString host, quint16 port_n)
{
QHostAddress addr(host);

client.connectToHost(addr,port_n);
}

void ETClient::start_transfer()
{
QString conSucc = "Connected successfully to: ";
QString device_addr = this->ui->txtbxIP->text();

this->ui->txtbxIP->setText(conSucc%device_addr);
this->ui->txtbxIP->setEnabled(false);
this->ui->txtbxPort->setEnabled(false);

this->ui->pushConnect->setShown(false);
this->ui->pushDisconnect->setShown(true);

this->ui->chkDIO0->setEnabled(true);
this->ui->chkDIO1->setEnabled(true);
this->ui->chkDIO2->setEnabled(true);
this->ui->chkDIO3->setEnabled(true);

this->on_pushSendData_clicked();
}

void ETClient::connection_invalid()
{
QString strInvalid = "Failed to connect. ";
QString device_addr = this->ui->txtbxIP->text();

this->ui->txtbxIP->setText("Invalid IP.");
this->ui->txtbxIP->isActiveWindow() == true;
}

void ETClient::close_session()
{
QString device_addr = this->ui->txtbxIP->text();

client.abort();

this->ui->txtbxIP->setEnabled(true);
this->ui->txtbxPort->setEnabled(true);

this->ui->pushDisconnect->setShown(false);
this->ui->pushConnect->setShown(true);

this->ui->chkDIO0->setEnabled(false);
this->ui->chkDIO1->setEnabled(false);
this->ui->chkDIO2->setEnabled(false);
this->ui->chkDIO3->setEnabled(false);
}

void ETClient::on_pushSendData_clicked()
{
bool dio0 = this->ui->chkDIO0->isChecked();
bool dio1 = this->ui->chkDIO1->isChecked();
bool dio2 = this->ui->chkDIO2->isChecked();
bool dio3 = this->ui->chkDIO3->isChecked();

if(dio0 == true) this->dio0_enabled();
if(dio1 == true) this->dio1_enabled();
if(dio2 == true) this->dio2_enabled();
if(dio3 == true) this->dio3_enabled();
else;
}

/*
*
* DIO management functions (DIO 0,1,2,3 ENABLED & DISABLED)
*
*/

void ETClient::dio0_enabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(nullptr); // DIO number (0 = 1ste, 1 = 2de, 2 = 3de, 3 = 4de)
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0 = aan, 1 = uit)

client.write(bytes);
}

void ETClient::dio1_enabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(1); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)

client.write(bytes);
}

void ETClient::dio2_enabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(2); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)

client.write(bytes);
}

void ETClient::dio3_enabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(3); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(nullptr); // signal (0, 1)

client.write(bytes);
}


void ETClient::dio0_disabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(nullptr); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)

client.write(bytes);
}

void ETClient::dio1_disabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(1); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)

client.write(bytes);
}

void ETClient::dio2_disabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(2); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)

client.write(bytes);
}

void ETClient::dio3_disabled()
{
int nullptr = 0;
QByteArray bytes;

bytes.push_back(2); // command number
bytes.push_back(2); // version
bytes.push_back(nullptr); // byte used for response
bytes.push_back(3); // data length
bytes.push_back(3); // DIO number
bytes.push_back(1); // i/o mode (0 input, 1 output)
bytes.push_back(1); // signal (0, 1)

client.write(bytes);
}


Thanks for your time!

ChrisW67
17th October 2011, 10:42
My function checks whether the checkbox is enabled, and if so, send the bytes. However, it only seems to work when using one checkbox at a time. I want to be able to select all of them, so all four functions are processed, and thus, all four LEDs go on (or off).
Nothing in that code would appear to turn LEDs off (assuming dioX_enabled() turns them on).


Any suggestions? I can't seem to see why it does that.

You have never initialised the bools, so they contain whatever rubbish data is in the memory location allocated to them until you set them.

-Kyr0s-
17th October 2011, 12:28
Nothing in that code would appear to turn LEDs off (assuming dioX_enabled() turns them on).
That's correct, yes. I've not used the dioX_disabled() functions as of yet, as I want to make sure that dioX_enabled() works before doing so. I use another app (not made by me) to disable the LEDs in-case needed.

You have never initialised the bools, so they contain whatever rubbish data is in the memory location allocated to them until you set them.
I'm not quite sure what you mean. I want to be able to select or deselect them when connecting, without having set them to enabled – they need to be unchecked by default when starting the application/connecting.

I've tried however, to do so as you suggested, but the same issue occurs: only one is being processed – so, for instance, it's not possible to select all four of them, and enable all LEDs. Only the first is being turned on (or the second, third, fourth; just as long as it's only one checkbox).