Get target of cascades TouchEvent not working - c++

I'm having problems trying to reach for the target of the Touch Event from BB10 cascades API. I have several containers, one below the other, and each one of them have the same Touch signal and slot assigned. Everything is being dynamically loaded from C++. So, in order to catch each touch event, I need to know which container triggered the event. I've read that I just need to use the TARGET property from the TouchEvent, but is not working and I dont know why. So I'm asking for help
Here's my code:
for (int i = 0; i < 10; i++) {
QmlDocument *qml = QmlDocument::create("asset:///customComponents/TableRow.qml").parent(this);
Container *passivesRow = qml->createRootObject<Container>();
passivesRow->setProperty("labelTextOne", "Hello_" + i);
bool res = QObject::connect(passivesRow,
SIGNAL(touch(bb::cascades::TouchEvent*)), this,
SLOT(handleAccountTouch(bb::cascades::TouchEvent*)));
Q_ASSERT(res);
Q_UNUSED(res);
myCurrentPageContainer->add(passivesScroll);
}
void PosicionConsolidada::handleAccountTouch(bb::cascades::TouchEvent* event) {
if (event->touchType() == TouchType::Up) {
qDebug() << "event catched";
VisualNode *p = event->target();
qDebug() << "object p: " << p->property("labelTextOne"); //Print nothing
}
}
Everything else is working just fine. My list of Containers is being created fine with their respective texts. When I click one of them, the event is being catched successfuly. I also tried to cast the VisualNode object to Container and it didn't work either. Please help!.

I would advice you an alternate approach which I used before. You can set objectName of control like this:
passiveRow->setObjectName("Hello_" + i");
QObject::connect(passiveRow, SIGNAL(touch(bb::cascades::TouchEvent*)), this,
SLOT(handleAccountTouch(bb::cascades::TouchEvent*)));
& in SLOT using it you can know which control emitted the signal:
if (event->touchType() == TouchType::Up) {
qDebug() << "object: " << QObject::sender()->objectName();
}
Here, sender() returns the control which emitted the signal.

In the API reference, there is no onTouch signal for the container.
On the contrary of other element like CustomControl
I do not know how your signal was successfully connected to your slot, but I guess that it was propagated from another component inside the container. So the target may be a Label or something else inside it.

Related

Issues trying to make a filter using Qt

I'm trying to make my software filter a list of elements.
I think I need to put my data in a TableWidget because I have to display other information.
I also put 3 Line Edit to search respectively in each category (called leName, leSource, leDestination)
so I tried to implement my filter for the name property, for now I have :
void MyClass::on_leName_textEdited(const QString &arg1)
{
for (int i=0;ui->tableWidget->rowCount()-1;i++)
{
qDebug()<<"before if";
if(ui->tableWidget->item(0,1)->text().contains(arg1) //tried with &arg1, I have the same issue, for now
{
qDebug()<<"If validated";
ui->tableWidget->showRow(i);
}else{
qDebug()<<"If not validated"
ui->tableWidget->hideRow(i)
}
}
}
When I hit a key, my soft crashes
I get "Before if", "If (not) validated", "Before if" from qDebug
I launched with debbugger and got Segmentation fault as error
not sure what I could add as detail about my error
Maybe I did nothing in the good way, I'm no expert in Qt nor c++
If you have any idea of what I should do to correct this, I would appreciate it =)
The fact that a cell exists does not imply that it has an associated QTableWidgetItem, so you must verify that it is not null.
QTableWidgetItem * item = ui->tableWidget->item(0, 1);
if(item && item->text().contains(arg1))
{
qDebug() << "If validated";
ui->tableWidget->showRow(i);
}else{
qDebug() << "If not validated"
ui->tableWidget->hideRow(i)
}

Is there a way to effectively update CAN data in QML?

Currently I am receiving CAN data in real time using socketcan API in main.cpp file.
I am constantly updating the data frame of CAN in one variable in main.cpp.
I want to express the gauge in real time by passing the variable containing the CAN data frame in main.cpp to the QML animation gauge.
I need to detect the change of the variable containing CAN data in QML in real time. I wonder if there is an effective way.
I tried to share data with QML using emit.
However, the function written by emit inside device-> connect (device, & QCanBusDevice :: framesReceived, [device] () {...} does not work.
When using it, I get the error
'this' cannot be implicitly captured in this context.
I looked up the error, but did not find the answer.
if (QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) {
qWarning() << "plugin available";
}
QString errorString;
QCanBusDevice *device = QCanBus::instance()->createDevice(
QStringLiteral("socketcan"), QStringLiteral("vcan0"), &errorString);
if (!device) {
qWarning() << errorString;
} else {
device->connectDevice();
std::cout << "connected vcan0" << std::endl;
device->connect(device, &QCanBusDevice::framesReceived, [device]() {
QCanBusFrame frame = device->readFrame();
QString testV = frame.toString();
QString qvSpeed = frame.payload();
std::string text = testV.toUtf8().constData();
std::string vSpeed = qvSpeed.toUtf8().constData();
//At that point the vVal values ​​are being updated in real time.
//I want to pass the updated vVal to qml gui in real time.
int vVal = static_cast<int>(frame.payload()[0]);
//emit sendMessage(vVal); // 'this' cannot be implicitly captured in this context error.
std::cout << text << std::endl;
});
}
As of now, main.cpp can't send the data and QML can't solve the error.
Inside device-> connect, emit sendMessage (vVal); will cause "'this' cannot be implicitly captured in this context" error.
I'm wondering if there is a good way to implement animation by expressing QML GUI data in real time.
Your capture clause only captures device. You need to also explicitly capture this:
device->connect(device, &QCanBusDevice::framesReceived, [this,device]{ /*...*/ });
BTW, note that there's no need to specify the () for a no-args lambda expression.

Qt Crash when listWidget item is clicked twice

In my project, I have a listWidget. When the user clicks an item in the list, it loads this:
void BlockSelect::on_blockList_clicked(const QModelIndex &index)
{
QString blockListName;
QString temp_hex;
QString temp_hex2;
int temp_int;
QListWidgetItem *newitem = ui->blockList->currentItem();
blockListName = newitem->text();
temp_hex = blockListName.mid(0, blockListName.indexOf(" "));
if(temp_hex.indexOf(":") == -1)
{
temp_int = temp_hex.toInt();
ui->blockIdIn->setValue(temp_int);
ui->damageIdIn = 0;
}
else
{
temp_hex2 = temp_hex.mid(temp_hex.indexOf(":")+1, temp_hex.length()-(temp_hex.indexOf(":")+1));
temp_hex = temp_hex.mid(0, temp_hex.indexOf(":"));
temp_int = temp_hex.toInt();
ui->blockIdIn->setValue(temp_int);
temp_int = temp_hex2.toInt();
ui->damageIdIn->setValue(temp_int);
}
}
Most of this is just string manipulation. (You don't have need to study this syntax or anything)
My problem is, when the user clicks on another list item quickly (Before this current process is finished) the program crashes. Is there any way to allow fast clicks (multiple processes at once) or maybe an alternative solution?
Thanks for your time :)
I hope that you execute all this code in the GUI thread. If so, then there will be no problem - if your code were correct (it isn't). There is no such thing as the "process" that you mention in your question. The clicks are handled by a slot, and they are invoked from an event handler within the list. This is not supposed to crash, and the clicks will be handled in a serialized fashion - one after another.
Here's the bug: Why do you reset the value of an allocated UI pointer element to zero?
ui->damageIdIn = 0;
This is nonsense. Maybe you mean to ui->damageIdIn->setValue(0) or ui->damageIdIn->hide(). You then proceed to use this zero value in
ui->damageIdIn->setValue(temp_int);
and it crashes.
You may also have bugs in other places in your code.

How to disable next button in QWizard

What I'm trying to do
I am trying to create a subclass of QWizardPage that looks somewhat like this,but has a slight tweak. I want to disable the next button when a counter variable is more than 0. (It can't be 0 from the get-go due to some functionality that requires it to go x..x-1...0).
What I've tried
Reimplement isComplete() and emit completeChanged() in the constructor
bool DemoWizardPage::isComplete()
{
return ! (counter > 0); //Also tried just return false;
}
Reimplement initializePage and disable the next button from there
void DemoWizardPage::initializePage()
{
qDebug() << "QWizardPage:: initialize page";
if (!this->isComplete())
{
qDebug() << "try to turn off next button";
wizard()->button(QWizard::NextButton)->setDisabled(true);
qDebug() << "next button enabled? "
<< wizard()->button(QWizard::NextButton)->isEnabled();
}
}
Results so far
From stepping through the code I can see that the next button is disabled when the page loads. But then it is enabled again due to these 2 lines in QWizardPrivate (taken from qwizard.cpp)
bool complete = page && page->isComplete();
btn.next->setEnabled(canContinue && complete);
I am quite baffled as to why isComplete() is returning true here. I mean, I set my counter to be 2 at the beginning and I never decrease it. (And yes, I do emit a completeChanged() whenever I set the counter).
Any ideas?
QWizard automatically manages the state of Next button based on the QWizardPage::isComplete(), you should not implement that functionality yourself in initializePage(). The reason your isComplete() is not being called is that it doesn't actually overwrite QWizardPage::isComplete const from QWizardPage. Declare your function const and it will properly overwrite the original function.

Capturing modifier keys Qt

I am trying to understand how to handle various events with Qt and have found an issue I cannot understand with key modifiers e.g. Ctrl Shift Alt etc. I have made a default Qt GUI Application in Qt Creator extending QMainWindow and have found that the following example does not produce understandable results.
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug() << "Modifier " << event->modifiers().testFlag(Qt::ControlModifier);
qDebug() << "Key " << event->key();
qDebug() << "Brute force " << (event->key() == Qt::Key_Control);
}
Using the modifiers() function on the event never is true while the brute force method returns the correct value.
What have I done wrong?
Try using this to check for shift:
if(event->modifiers() & Qt::ShiftModifier){...}
this to check for control:
if(event->modifiers() & Qt::ControlModifier){...}
and so on. That works well for me.
EDIT:
To get the modifiers of a wheel event, you need to check the QWheelEvent object passed to your wheelEvent() method:
void MainWindow::wheelEvent( QWheelEvent *wheelEvent )
{
if( wheelEvent->modifiers() & Qt::ShiftModifier )
{
// do something awesome
}
else if( wheelEvent->modifiers() & Qt::ControlModifier )
{
// do something even awesomer!
}
}
According to the documentation, QKeyEvent::modifiers cannot always be trusted. Try to use QApplication::keyboardModifiers() static function instead.
From Qt 5 Doc. – Qt::KeyboardModifiers QKeyEvent::modifiers() const:
Warning: This function cannot always be trusted. The user can confuse it by pressing both Shift keys simultaneously and releasing one of them, for example.