one item for 5 positions - qt

I have a table. And an item.
How can I use only one Item to fill part of the table?
If I set item to one position and then take item, I lose text at that position.
And use lots of items is not comfortable.
QTableWidgetItem *Type = new QTableWidgetItem;
if( line.contains("some"))
{
Type->setText("some");
ui->tableWidget->setItem(i, 0, Type);
}
else if( line.contains("shi"))
{
Type->setText("shi");
ui->tableWidget->setItem(i, 0, Type);
}
ui->tableWidget->takeItem(i, 0);

You can create copies of the item to insert at different cells. This can be done using clone :
QTableWidgetItem *Type1 = new QTableWidgetItem;
Type1->setText("some");
ui->tableWidget->setItem(row1, col1, Type1);
QTableWidgetItem *Type2 = Type1->clone(); // create a copy
Type2->setText("shi");
ui->tableWidget->setItem(row2, col2, Type2);

Related

Get The Row,Column values from a cell in QT

I have a problem, i want to return the selected Rows values, and the columns separately, i found a method to return both of them using the function cell(row, column), but i want to get them separately
Here is my code :
QTableWidgetItem *c = new QTableWidgetItem();
QMap<QString,int> lists;
for(i=0;i<range.rowCount();++i){
for(int j=0;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
QMessageBox::information(this,"",c->text());
}
}
As you can see this code is working, but i just want to return the Rows and the Columns separately so i can put them in my QMap<QString,int> list.
And the purpose of all this is to try to draw a piechart from the selected rows and columns
So Any help please
Here is what I understood from the comments, feel free to correct me and I'll update my answer if necessary.
COL1 | COL2
NAME | VALUE
So when you select a cell, you actually care about the whole row, a.k.a the name of the row and the value associated. If this is the case, it would make more sense to only allow the user to select whole rows, instead of cells. setSelectionBehavior(QAbstractItemView::SelectRows); should do the trick.
Provided that the name of the dataset is always in column 1, and the value in column 2, you should update your code with the snippet:
QTableWidgetItem *c; //Deleted memory leak in your code.
QMap<QString,double> myMap; //Don't name it a list if it is explicitly a map.
for(i=0;i<range.rowCount();++i){
QString dataName = item(i,0)->text();
int dataValue;
for(int j=1;j<range.columnCount();++j){
c=item(i,j);// here i can return the Rows, Columns Data
dataValue += c->text().toDouble();
//If you always have 2 columns only, dataValue will be the value you are looking for.
//If you can have more than 2 columns, dataValue will be the sum of all the cells located after the column 0, on the same row.
//Change this depending on how you want to treat those values.
QMessageBox::information(this,dataName,c->text());
}
myMap[dataName]=dataValue;
}
EDIT for QPieSeries, following this example:
QPieSeries *series = new QPieSeries();
QMap<QString,double>::iterator it = myMap.begin();
QMap<QString,double>::iterator end = myMap.end();
for(; it!=end; ++it){
series->append(it->key(), it->value());
}
QPieSlice *slice = series->slices().at(1);
slice->setExploded();
slice->setLabelVisible();
slice->setPen(QPen(Qt::darkGreen, 2));
slice->setBrush(Qt::green);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("My Data");
chart->legend()->hide();
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
/*change with your window here*/
yourWindow.setCentralWidget(chartView);

update edited cells within treetable after expand/collapse items

I have a treeTable with editable cells within the expanded rows. The editable cells get a dirty flag after editing (in the example the background color is set to red).
The problem i'm running into is that i found no certain way to update the dirty flag on expand/collapse (edited cells get the css class 'edited-cell').
At the moment the code looks like that:
// each editable textfield gets a Listener
textField.attachLiveChange(
var source = oEvent.oSource;
...
jQuery('#' + source.getId()).addClass(EDITED_CELL_CLASS, false)
// list with cell ids, e.g. "__field1-col1-row1"
DIRTY_MODELS.push(model.getId()) //*** add also binding context of row
)
// the table rows are updated on toggleOpenState
new sap.ui.table.TreeTable({
toggleOpenState: function(oEvent) {
...
this.updateRows() // see function below
}
})
// update Rows function is also delegated
oTable.addDelegate({ onAfterRendering : jQuery.proxy(this.updateRows, oTable)});
//http://stackoverflow.com/questions/23683627/access-row-for-styling-in-sap-ui5-template-handler-using-jquery
// this method is called on each expand/collapse: here i can make sure that the whole row has it's correct styling...
// but how to make sure that special cells are dirty?
function updateRows(oEvent) {
if (oEvent.type !== 'AfterRendering'){
this.onvscroll(oEvent);
}
var rows = this.getVisibleRowCount();
var rowStart = this.getFirstVisibleRow();
var actualRow;
for (var i = 0; i < rows; i++){
actualRow = this.getContextByIndex(rowStart + i); //content
var row = this.getRows()[i]
var obj = actualRow.getObject()
var rowId = row.getId()
updateStyleOfRows(obj, rowId, actualRow)
updateDirtyCells(rowId) //*** how to get the binding context in this function???
}
};
// update Dirty Cells in function updateRows():
function updateDirtyCells(rowId){
for (var i = 0; i < DIRTY_MODELS.length; i++){
var dirtyCellId = DIRTY_MODELS[i]
//*** make sure that only the correct expanded/collapsed rows will be updated -> depends on the bindingContext of the row
jQuery('#' + rowId).find('#' + dirtyCellId + '.editable-cell').addClass(EDITED_CELL_CLASS, false)
}
}
This doesn't work correctly, because the ids of the cells change on each layout render (e.g. collapse/expand rows). Please see attached image.
Let me know if i should provide more information.

How to simply delete row in QFormLayout programatically

I have this code:
myEdit = QLineEdit()
myQFormLayout.addRow("myLabelText", myEdit)
Now I have to remove the row by reference to myEdit only:
myQformLayout.removeRow(myEdit)
But there is no API for that. I can use .takeAt(), but how can I get the argument? How do I find the label index, or the index of myEdit?
You can just schedule the widget and its label (if it has one) for deletion, and let the form adjust itself accordingly. The label for the widget can be retrieved using labelForField.
Python Qt code:
label = myQformLayout.labelForField(myEdit)
if label is not None:
label.deleteLater()
myEdit.deleteLater()
my solution...
in header file:
QPointer<QFormLayout> propertiesLayout;
in cpp file:
// Remove existing info before re-populating.
while ( propertiesLayout->count() != 0) // Check this first as warning issued if no items when calling takeAt(0).
{
QLayoutItem *forDeletion = propertiesLayout->takeAt(0);
delete forDeletion->widget();
delete forDeletion;
}
This is actually a very good point... there is no explicit reverse function for addRow().
To remove a row you can do the following:
QLineEdit *myEdit;
int row;
ItemRole role;
//find the row
myQFormLayout->getWidgetPosition( myEdit, &row, &role);
//stop if not found
if(row == -1) return;
ItemRole otheritemrole;
if( role == QFormLayout::FieldRole){
otheritemrole = QFormLayout::LabelRole;
}
else if( role == QFormLayout::LabelRole){
otheritemrole = QFormLayout::FieldRole;
}
//get the item corresponding to the widget. this need to be freed
QLayoutItem* editItem = myQFormLayout->itemAt ( int row, role );
QLayoutItem* otherItem = 0;
//get the item corresponding to the other item. this need to be freed too
//only valid if the widget doesn't span the whole row
if( role != QFormLayout::SpanningRole){
otherItem = myQFormLayout->itemAt( int row, role );
}
//remove the item from the layout
myQFormLayout->removeItem(editItem);
delete editItem;
//eventually remove the other item
if( role != QFormLayout::SpanningRole){
myQFormLayout->removeItem(otherItem);
delete otherItem
}
Note that I retrieve all the items before removing them. That's because I don't know if their role will change when an item is removed. This behavior is not specified so I am
playing safe. In qt designer, when you remove an item from a form, the other item on
the row take all the space (which means his role changes...).
Maybe there is a function somewhere and not only I reinvented the wheel but I made a broken one...

How to repace an element in a Qt tree widget?

I have a Qt application that contains a QTreeWidget.
I want to replace one of the items.
I have a pointer:
QTreeWidgetItem *elementToSubstitute;
and a function that returns a QTreeWidgetItem*.
I want to overwrite the previous one with this new element, in the same place.
If I delete the previous element and insert the new one after that, the new item is placed at the bottom of the tree.
How should I proceed to have the new branch replace exactly the previous one?
EDIT for example
void MainWindow::addRoot(QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->tree);
itm->setText(0,name);
itm->setText(1,description);
QTreeWidgetItem *child1 = addChild(itm,"one","hello");
QTreeWidgetItem *child2 = addChild(itm,"two","hello");
QTreeWidgetItem *child3 = addChild(itm,"tree","hello");
QTreeWidgetItem *child4 = addChild(itm,"four","hello");
QTreeWidgetItem *parent = child2->parent();
int itemIndex = parent-indexOfChid(child2);
parent->removeChild(child2);
parent->insertChild(itemIndex, child4 );
}
QTreeWidgetItem MainWindow::addChild(QTreeWidgetItem *parent,QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem();
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled );
itm->setText(0,name);
itm->setText(1,description);
addChild2(itm,"sub-one","subchild2");
addChild2(itm,"sub-two","subchild2");
addChild2(itm,"sub-tree","subchild2");
parent->addChild(itm);
return itm;
}
void MainWindow::addChild2(QTreeWidgetItem *parent,QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem();
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
itm->setText(0,name);
itm->setText(1,description);
itm->setText(2,"test third coloumn");
parent->addChild(itm);
}
.....
Now i have a pointer to one of the chiled. ( let's sa of level 1) ans i want to replace it with another. for instance i want to replace child "2" with child "4" but i want this to be put in the place f child 2.
You could try doing something like this:
QTreeWidgetItem *parent = elementToSubstitute->parent();
int itemIndex = parent->indexOfChid(elementToSubstitute);
parent->removeChild(elementToSubstitute);
parent->insertChild(itemIndex, yourNewTreeItem);
Note: if yourNewTreeItem is already in the tree, this will have no effect on its position, i.e. it will not duplicate a node.
If your item is top-level then you could use indexOfTopLevelItem to get it's index, then remove it from tree widget, then insert replacement item using insertTopLevelItem passing index you obtained. If you item is not top-level then you could use indexOfChild and insertChild.
Why do you need to replace the whole item? Just change the contents of the current item, or copy the new item into the current one.
//assuming you have a pointer to the new and the current Item
*currentItem = *newItem; //done

Datagrid with Checkbox

suppose consider in datagrid i have two text box when i fill records to it then i ill press tab key then another row automatically generated so how can i delete multiple rows using checkbox in datagrid not in gridview
int i = (DataGridView1.Rows.Count - 1); while ((i >= 0)) { i = (i -
1); foreach (DataGridViewRow dr in DataGridView1.Rows) { if
(dr.Cells("check").Value == false) {
DataGridView1.Rows.Remove(dr); } } }

Resources