Question

I am using a QProcess to scan the available bluetooth devices. The QProcess uses command line commands like hcitool scan to check for available devices. The output is parsed using regular expressions and I have cells(equal to number of devices scanned) of datastructure with details like device name, channel for OPUSH, type of device, device address.

To display all the devices I use QListWidget

    //Display on list widget
    //Send all information regarding device name, address, OPUSH channel and icon
    //to child widget myBTwidget
    myBTwidget *MyItem = new myBTwidget();

    MyItem->BTName(DeviceName);

    MyItem->BTAddress(address);

    MyItem->BTChannelNo(channel);

    MyItem->BTIcon(icon);

    QListWidgetItem *item=new QListWidgetItem();

    item->setSizeHint(QSize(720,50));

    this->ui->listWidget->addItem(item);

    //Add child widget myBTwidget to list widget
    this->ui->listWidget->setItemWidget(item,MyItem);

The custom child widget has 1. a label showing the image for the type of device 2. a button (flat) displaying the device name 3. a label stating if OPUSH channel to send files is available or not

The listwidgetitem pushbutton is clicked, it pairs the with the bluetooth device and sends files. I would like to connect QListwidgetitem clicked() signal to the childwidget pushbutton containing the device name clicked() slot. In this way the user can press anywhere on the item and the device pairs and sends file.

How do i go about it?

The listwidget is in the parent class,and the item is a child widget. I tried connecting the clicked signal of qlistwidgetitem to a slot onitemclicked(). In onitemclicked() function I tried calling the function within custom childwidget that implements the pushbutton clicked function, but since it wouldnt have details like device name, Address, channel it cannot do the operation.

If I were to simply click on the child widget pushbutton containing the text as device name, it has all the details and will do the pairing and send operation.

I thought of few solutions, 1.create a global structure and save the data in it and utilize OR 2. create a table in database and save the data while parsing loading child widgets and use this data to call the function to send the file

I would like to know is there a better way to go about it? If I can have individual listwidgetitem details sent to custom child widget stored in someway and on clicking the item, get all the details and call the function to do the pairing and send file??

Kindly please help me out.

Thank you

Was it helpful?

Solution

first of all: QListWidgetItem does not have a clicked() signal. (It is not a widget itself) You should connect to QListWidget::itemClicked(QListWidgetItem*) - As you can see, this signal provides you the specific Item that has been clicked.

Of course, this alone won't help you, as the Item doesn't know the data displayed. You should store the information in the QListWidgetItem itself, using setData(), so you can get it later on from the Item.

But as you probably don't want to double store your data (once in myBTwidget and once in QListWidgetItem), you could store data only in the QListWidgetItem, and refactor your widget into a delegate...

Have a look in the documentation for delegates, you'll find some exaples there. (I am not sure if delegates work with a QListWidget without problems, using a QListView with a QStandardItemModel/custom model might be the better choice)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top