PDA

View Full Version : QSocketNotifier enabled or disabled from another thread



johnsoga
26th September 2017, 19:33
I know this question has come up before but I can't get ride of the warning

QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

I have a button class that monitors a GPIO line and emits a signal when a falling edge is detected. I am using a QSocketNotifier to monitor the gpio dev device. Following is my code in the button class for opening the devices and setting up the QSocketNotifier:


void Button::Open()
{
int retval;

d->m_fd = open(d->m_device.toLatin1().data(), O_RDONLY);
if (d->m_fd < 0)
{
qDebug() << "Failed to open";
return;
}

retval = ioctl(d->m_fd, GPIO_GET_LINEEVENT_IOCTL, &d->m_request);
if (retval < 0)
{
qDebug() << "Failed to ioctl";
close(d->m_fd);
return;
}

d->m_notifier_p = new QSocketNotifier(
d->m_fd, QSocketNotifier::Read, this);
connect(d->m_notifier_p, &QSocketNotifier::activated,
this, &Button::OnActivated);
}


I am then creating the button in another class and moving it to its own thread.


Button *btn2_p = new Button(BUTTON_DEVICE, BTN2_LINE, 1);
btn2_p->moveToThread(&m_btn2_thread);
connect(&m_btn2_thread, &QThread::finished, btn2_p, &Button::deleteLater);
connect(&m_btn2_thread, &QThread::started, btn2_p, &Button::Open);
d->m_btn2_thread.start();;


The button does not have a parent and the open function should be getting called in the m_btn2_thread so the socket notifier is created in the m_btn2_thread.

What did I do wrong in my code to get this error message?