Skip to content

Commit aa0dec8

Browse files
committed
add setItemImage method to Table widget
1 parent 9cf41ad commit aa0dec8

9 files changed

+69
-2
lines changed

UIFunctions.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ void UIFunctions::connectSignals()
156156
#endif
157157
#if WIDGET_TABLE
158158
connect(this, &UIFunctions::setItem, uiproxy, &UIProxy::onSetItem, Qt::BlockingQueuedConnection);
159+
connect(this, &UIFunctions::setItemImage, uiproxy, &UIProxy::onSetItemImage, Qt::BlockingQueuedConnection);
159160
connect(this, &UIFunctions::setRowHeaderText, uiproxy, &UIProxy::onSetRowHeaderText, Qt::BlockingQueuedConnection);
160161
connect(this, &UIFunctions::setColumnHeaderTextTable, uiproxy, &UIProxy::onSetColumnHeaderTextTable, Qt::BlockingQueuedConnection);
161162
#endif

UIFunctions.h

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private slots:
174174
void setRowCount(Table *table, int count, bool suppressSignals);
175175
void setColumnCountTable(Table *table, int count, bool suppressSignals);
176176
void setItem(Table *table, int row, int col, std::string text, bool suppressSignals);
177+
void setItemImage(Table *table, int row, int col, std::string data, int width, int height, bool suppressSignals);
177178
void setRowHeaderText(Table *table, int row, std::string text);
178179
void setColumnHeaderTextTable(Table *table, int col, std::string text);
179180
void setItemEditable(Table *table, int row, int col, bool editable);

UIProxy.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,11 @@ void UIProxy::onSetItem(Table *table, int row, int column, std::string text, boo
10631063
table->setItem(row, column, text, suppressSignals);
10641064
}
10651065

1066+
void UIProxy::onSetItemImage(Table *table, int row, int column, std::string data, int width, int height, bool suppressSignals)
1067+
{
1068+
table->setItemImage(row, column, data, width, height, suppressSignals);
1069+
}
1070+
10661071
void UIProxy::onSetRowHeaderText(Table *table, int row, std::string text)
10671072
{
10681073
table->setRowHeaderText(row, text);

UIProxy.h

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public slots:
179179
void onSetRowCount(Table *table, int count, bool suppressSignals);
180180
void onSetColumnCountTable(Table *table, int count, bool suppressSignals);
181181
void onSetItem(Table *table, int row, int column, std::string text, bool suppressSignals);
182+
void onSetItemImage(Table *table, int row, int column, std::string data, int width, int height, bool suppressSignals);
182183
void onSetRowHeaderText(Table *table, int row, std::string text);
183184
void onSetColumnHeaderTextTable(Table *table, int column, std::string text);
184185
void onSetItemEditable(Table *table, int row, int column, bool editable);

callbacks.xml

+35
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,41 @@
16521652
<return>
16531653
</return>
16541654
</command>
1655+
<command name="setItemImage">
1656+
<description>Change item image in the specified table widget.</description>
1657+
<categories>
1658+
<category name="table" />
1659+
<category name="widgets" indirect="true" />
1660+
</categories>
1661+
<params>
1662+
<param name="handle" type="int">
1663+
<description>ui handle</description>
1664+
</param>
1665+
<param name="id" type="int">
1666+
<description>widget id</description>
1667+
</param>
1668+
<param name="row" type="int">
1669+
<description>row index</description>
1670+
</param>
1671+
<param name="column" type="int">
1672+
<description>column index</description>
1673+
</param>
1674+
<param name="data" type="string">
1675+
<description>image data</description>
1676+
</param>
1677+
<param name="width" type="int">
1678+
<description>image width</description>
1679+
</param>
1680+
<param name="height" type="int">
1681+
<description>image height</description>
1682+
</param>
1683+
<param name="suppressEvents" type="bool" default="true">
1684+
<description>if true, no event will be generated from this call</description>
1685+
</param>
1686+
</params>
1687+
<return>
1688+
</return>
1689+
</command>
16551690
<command name="getRowCount">
16561691
<description>Get the table widget number of rows.</description>
16571692
<categories>

tests/test-table-image.ttt

240 KB
Binary file not shown.

v_repExtCustomUI.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ void setItem(SScriptCallBack *p, const char *cmd, setItem_in *in, setItem_out *o
841841
#endif
842842
}
843843

844+
void setItemImage(SScriptCallBack *p, const char *cmd, setItemImage_in *in, setItemImage_out *out)
845+
{
846+
#if WIDGET_TABLE
847+
Table *table = getWidget<Table>(in->handle, in->id, cmd, "table");
848+
UIFunctions::getInstance()->setItemImage(table, in->row, in->column, in->data, in->width, in->height, in->suppressEvents);
849+
#endif
850+
}
851+
844852
void getRowCount(SScriptCallBack *p, const char *cmd, getRowCount_in *in, getRowCount_out *out)
845853
{
846854
#if WIDGET_TABLE

widgets/Table.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void Table::parse(Widget *parent, std::map<int, Widget*>& widgets, tinyxml2::XML
4848
std::string tag2(e2->Value() ? e2->Value() : "");
4949
if(tag2 != "item") continue;
5050
TableItem item;
51-
item.text = std::string(e2->GetText());
51+
item.text = std::string(e2->GetText() ? e2->GetText() : "");
5252
item.editable = xmlutils::getAttrBool(e2, "editable", true);
5353
rows[rows.size()-1].push_back(item);
5454
}
@@ -177,7 +177,22 @@ void Table::setItem(int row, int column, std::string text, bool suppressSignals)
177177
{
178178
QTableWidget *tablewidget = static_cast<QTableWidget*>(getQWidget());
179179
bool oldSignalsState = tablewidget->blockSignals(suppressSignals);
180-
tablewidget->setItem(row, column, new QTableWidgetItem(QString::fromStdString(text)));
180+
QTableWidgetItem *item = new QTableWidgetItem;
181+
item->setText(QString::fromStdString(text));
182+
tablewidget->setItem(row, column, item);
183+
tablewidget->blockSignals(oldSignalsState);
184+
}
185+
186+
void Table::setItemImage(int row, int column, std::string data, int width, int height, bool suppressSignals)
187+
{
188+
QTableWidget *tablewidget = static_cast<QTableWidget*>(getQWidget());
189+
bool oldSignalsState = tablewidget->blockSignals(suppressSignals);
190+
QTableWidgetItem *item = new QTableWidgetItem;
191+
QImage::Format format = QImage::Format_RGB888;
192+
int bpp = 3; // bytes per pixel
193+
QImage image((unsigned char *)data.data(), width, height, bpp * width, format);
194+
item->setData(Qt::DecorationRole, QPixmap::fromImage(image));
195+
tablewidget->setItem(row, column, item);
181196
tablewidget->blockSignals(oldSignalsState);
182197
}
183198

widgets/Table.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Table : public Widget, public EventOnKeyPress
5353
void setRowCount(int count, bool suppressSignals);
5454
void setColumnCount(int count, bool suppressSignals);
5555
void setItem(int row, int column, std::string text, bool suppressSignals);
56+
void setItemImage(int row, int column, std::string data, int width, int height, bool suppressSignals);
5657
int getRowCount();
5758
int getColumnCount();
5859
std::string getItem(int row, int column);

0 commit comments

Comments
 (0)