PDA

View Full Version : How to connect two QListwidgets which have some data in it?



allulks
11th November 2015, 19:34
I have read the data from file to Qlistwidgets and now I want to do the multiple selection (using control + mouse left click) randomly 2 or 3 rows and copy the data to another listwidgets. I want the same row number selected in both list widgets.

code for reading the data to list widgets, I didn't know how to proceed further to complete the task

QString filename=QFileDialog::getOpenFileName(this, tr("Open File"), "media://", "All files (.);; Text File (*.txt)");

QFile file3(filename);

if(!file3.open(QFile::ReadOnly|QFile::Text))
QMessageBox::information(0,"info", file3.errorString());

else
{ QTextStream in3(&file3);

while (!in3.atEnd()) {

QString line = in3.readLine();

QStringList list = line.split(" ");

ui->xvalue->addItem(list[0]);
ui->yvalue->addItem(list[1]);


}
}

ChrisW67
11th November 2015, 19:49
If you always want the x and y values to be selected together then are you sure you don't want to put the data in the same QTableWidget (or model and QTableView)?

allulks
11th November 2015, 20:35
If you always want the x and y values to be selected together then are you sure you don't want to put the data in the same QTableWidget (or model and QTableView)?

Hello,)
Actually I don't have much idea about QTablewidget, I am very beginner to Qt. I will tell my task once, After copying the X & Y values to another (QListwidget or QTablewidget) I have to read them again and store into 1D arrays (like X[ ] and Y[ ]). So which ever option is able to do my task I am ready to take it.

I hope you understood my task!!! Thanks a lot..:)

ChrisW67
12th November 2015, 19:36
I would definitely use a QTableWidget then.
Two columns (x, y), n rows.
setSelectionBehavior() to Qt::SelectRows
setSelectionMode() to Qt::ExtendedSelection or Qt::MultiSelection
Access the selectionModel() or selectedItems() to determine what is selected.

allulks
12th November 2015, 23:23
Thank you.. I will try QTablewidget and let you know

allulks
16th November 2015, 16:24
Hello, I used QTableWidget and imported my data to the table and I could able to do multi selection of rows also by using the following code.

But Problem is I am not able to get the row indices (row numbers which I select), can you help me out in finding out the row indices. I am typing the code which I have wrote for getting indices numbers

ui->xvalue->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->xvalue->setSelectionMode(QAbstractItemView::MultiSelection );


QItemSelectionModel* selectionModel = ui->xvalue->selectionModel();

QModelIndexList selected = selectionModel->selectedRows();
int row;
foreach (QModelIndex index, selected) {

qDebug() << index.row();
row = index.row();


printf("\nrow = %d\n",row);

}

Nothing is getting printed as output.

If i give printf() outside the curl braces, output is showing as '0'. Dont know where I am going wrong
Thanks for your help.

ChrisW67
16th November 2015, 20:10
That is exactly what you would expect if no rows are selected. Another approach would be to see what QTableWidget::selectedIndexes() has to say.

The calls to setup the selection mode and behaviour are a one-time setup and should not be called every time you want to get the selected rows. It is quite possible these calls clear the selection.

allulks
3rd December 2015, 16:26
Hello,

I have done the above thing perfectly with out any problem.

Now other task is, " If I click one push-button all rows in the QTable widget should be selected ".

I got struck I don't know how to proceed, can you help me in solving this.

Thanks for your help!!!

ChrisW67
3rd December 2015, 19:53
The QTableWidget has a selectAll() slot inherited from a base class.

It also has the corner button that does this by default.
http://doc.qt.io/qt-4.8/qtableview.html#cornerButtonEnabled-prop

allulks
3rd December 2015, 20:46
Hello,

It worked thanks a lot :) . This is what I have done

void Widget :: on_pushButton_3_clicked() //All rows are selected
{
ui->xvalue->selectAll();
}



void Widget :: on_pushButton_2_clicked() // Selected rows are deselected
{
ui->xvalue->selectionModel()->clearSelection();
}

allulks
5th December 2015, 08:24
Hello,
I am using QCustomplot for plotting the graph. I have used the code which is available in the given link for plotting the graphs.

http://qcustomplot.com/index.php/tutorials/basicplotting

The problem what I am facing is I could not able to RESET the graph after plotting. I tried many ways for removing or clearing the graphs once reset button is clicked, unfortunately nothing has worked out.

following code:

ui->customplot->addGraph();
ui->customplot->graph(0)->setData(a, b);

ui->customplot->xAxis->setLabel("x"); //Naming X-axis
ui->customplot->yAxis->setLabel("y"); //Naming Y-axis

ui->customplot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::s sCircle));



ui->customplot->xAxis->setRange(-1000, 1000); //X-axis range
ui->customplot->yAxis->setRange(-1000, 1000); //Y-axis range
ui->customplot->graph(0)->rescaleAxes(); //rescaling axes

ui->customplot->addGraph();
ui->customplot->graph(1)->setData(a, d);
ui->customplot->xAxis->setLabel("x"); //Naming X-axis
ui->customplot->yAxis->setLabel("y"); //Naming Y-axis
ui->customplot->graph(1)->setPen(QPen(Qt::green));
ui->customplot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::s sCross));
ui->customplot->graph(1)->rescaleAxes();
ui->customplot->replot();
ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);


void Widget :: on_reset_clicked() //Reset calculation
{
ui->customplot->clearPlottables();

}
I have tried many like : cleargraphs(); cleardata(); removegraphs(); clearplottables(); nothing has worked out.

can you please suggest me some idea about how to reset the graphs. Thanks for your help.