spinbox input add rows ??
-
how to add rows based on spinbox input ?
i have qtableviiew so when user enter number in spinbox number. of rows should be added how to do this ?
as i can add row using 2 ways on button click1. add row{ model->insertRow(model->rowCount(QModelIndex())); } 2. QList<QStandardItem*> newRow; QStandardItem* item1 = new QStandardItem(QString("")); newRow.append(item1); model->appendRow(newRow);
how to do ??
thanks in advance
-
@n-2204
Either way, get the number value from the spin box and do a loop that many times to add multiple rows? There is also ainsertRows(int row, int count, const QModelIndex &parent = QModelIndex())
to insert multiple rows at a time.
-
@n-2204
https://doc.qt.io/qt-5/signalsandslots.htmlConnect to the
valueChanged(int)
signal https://doc.qt.io/Qt-5/qspinbox.html#valueChangedIt is up to you to figure out if value has increased or decreased but you can figure it out using
rowCount()
-
@artwaw
this is not working why??QObject::connect(ui.spinBox, SIGNAL(valueChanged(int)), ui.tableView, SLOT(setTableRows())); void tool::setTableRows() { int rows = ui.spinBox->value(); model->setRowCount(rows); }
-
@n-2204 said in spinbox input add rows ??:
this is not working why??
Because you did something wrong.
I would guess ui.tableView is not the correct receiver.Use the new signal/slot syntax to catch such errors during compile time.
-
tried after change ui.tableview to model but not working
QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); void tool::setTableRows() { int rows = ui.spinBox->value(); model->setRowCount(rows); }
or tried using insertrows also not working
QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); void tool::setTableRows() { int noofrows = ui.spinBox->value(); for (int i = 0;i < noofrows;++i) { model->insertRows(i, model->rowCount(QModelIndex())); } }
-
@n-2204
First, put in debug/print statements just to see if slot called, instead of relying on seeing row count increase.Secondly, you have
QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows()));
but
void tool::setTableRows()
Is
model
of classtool
? Because if not you are not connecting the slot.If you followed @Christian-Ehrlicher's suggestion of changing to new signal/slot syntax and never used
SIGNAL
/SLOT()
macros you would find out, and your coding would be better....And finally I doubt you really intend
model->insertRows(i, model->rowCount(QModelIndex()));
Why
rowCount()
as the number of rows to add?? I would have thought given your loop you would want1
there. You are confusinginsertRows()
to add multiple rows without a loop versus a loop to add one row at a time....
-
@JonB yes tool is my class
model = new QStandardItemModel(10, 13, this) ui.tableView->setModel(model);
-
@n-2204
You seem to have:QObject:: connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); void tool::setTableRows()
You have just said that
model
is of typeQStandardItemModel
.But
setTableRows()
is a method oftool
.And earlier you had:
QObject::connect(ui.spinBox, SIGNAL(valueChanged(int)), ui.tableView, SLOT(setTableRows()));
which had the same issue,
For the third time, if you followed https://wiki.qt.io/New_Signal_Slot_Syntax you would get compile-time help preventing you doing what you are trying to do.
-
I suspect the 3rd argument might be
this
. You can at least put theconnect
inside aQ_ASSUME()
so you get a runtime crash in debug mode if the connect fails
-
yes, thanks using this works
but what happening is i added 10 rows and when i change value in spinbox to 1 then no. of rows in table also changing to 1so i think using loop this prblm will be solved
int noofrows = ui.spinBox->value(); for (int i = 0;i < noofrows;i++) { model->insertRows(i,noofrows); }
if use this loop when i enter 1 in spinbox it adds two rows ?
-
@n-2204
I already told you about that earlier.And finally I doubt you really intend
model->insertRows(i, model->rowCount(QModelIndex()));
Why
rowCount()
as the number of rows to add?? I would have thought given your loop you would want 1 there. You are confusinginsertRows()
to add multiple rows without a loop versus a loop to add one row at a time....EITHER a
for
loop withinsertRow()
, OR nofor
loop andinsertRows()
. You really should be able to understand this.Put some
qDebug()
statements in to see for yourself what is going on. That is what debugging is about.
-
ok understood
thank you all
-
tooll::tool(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); model = new QStandardItemModel(10, 13, this) ui.tableView->setModel(model); connect(ui.spinBox, SIGNAL(valueChanged(int)), model, SLOT(setTableRows())); } void tool::setTable2Rows() { int noofrows2 = ui.spinBox_2->value(); for (int i = 0;i < noofrows2;++i) { model2->insertRow(i); //qDebug() << i; } }
when i am using new signal/slot why i am getting error
(Error (active) E0304 no instance of overloaded function)connect(ui.spinBox, &QSpinBox::valueChanged, this,&tool::setTableRows); connect( sender, &Sender::valueChanged, receiver, &Receiver::updateValue);
-
@n-2204 Please post whole error message. Does your tooll class have setTableRows slot? I'm asking because I only see setTable2Rows()
-
yes slot setTablerow() is also there
void tool::setTableRows() { int noofrows = ui.spinBox->value(); for (int i = 0;i < noofrows;++i) { model->insertRow(i); //qDebug() << i; }
c++ no instance of overloaded function matches the argument list argument types are: (QSpinBox *, <unknown-type>, tooll , void (tool::)())
Severity Code Description Project File Line Suppression State Detail Description
Error (active) E0304 no instance of overloaded function "tool::connect" matches the argument list tool C:\User\source\repos\tool\tooll.cpp 265 argument types are: (QSpinBox *, <unknown-type>, tool , void (tool::)())
-
@n-2204 said in spinbox input add rows ??:
tooll::tool(QWidget* parent)
????
What's the name of the class?
tooll
ortool
? looks like a big mess here
-
@n-2204 said in spinbox input add rows ??:
c++ no instance of overloaded function matches the argument list argument types are: (QSpinBox *, <unknown-type>, tooll , void (tool::)())
How about taking a single look into the documentation? The problem is exactly explained there.
Simply writing something without understanding what you're doing doesn't help you at all.
-
thanks for sharing ref doc, its done
connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTableRows);
-
what changes need to do to end the row at end of row, now when i enter value in spinbox row is adding in top but i need to add the row at end how to do ?
void tool::setTableRows() { int noofrows = ui.spinBox->value(); for (int i = 0;i < noofrows;++i) { model->insertRow(i); } } connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTableRows);
-
@n-2204 https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRow
"Inserts a single row before the given row in the child items of the parent specified".
So, what do you think should you pass as row to insertRow to add at the end of the table?
-
@jsulm said in spinbox input add rows ??:
@n-2204 https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRow
"Inserts a single row before the given row in the child items of the parent specified".
So, what do you think should you pass as row to insertRow to add at the end of the table?Further to @jsulm.
insertRow()
saysNote: This function calls the virtual method
insertRows
.And in the docs for
insertRows
, immediately adjacent toinsertRow
, if you read it it actually tells you what to pass so thatthe rows are appended to any existing rows
Please take the time to read docs....
-
how can i set value of a spinbox and also edittable for user ?
void tool::setTable2Rows() { QAbstractItemModel* table2 = ui.tableView_2->model(); int rows2 = ui.spinBox_2->value(); ui.spinBox->setValue(10);//this is not working model2->setRowCount(rows2); }
if i setvalue 10 then that no. of rows should be there
-
@n-2204 said in spinbox input add rows ??:
ui.spinBox->setValue(10);//this is not working
Don't what you mean. "Not working" could indicate anything. I imagine this works perfectly well.
how can i set value of a spinbox and also edittable for user ?
if i setvalue 10 then that no. of rows should be there
Don't know what these mean either.
-
not working means value is not set to 10
and when i setvalue as 10 , 10 rows should be there in my table
also when user change the spinbox value to 11 or 8 no. of rows in table should update
-
@n-2204 said in spinbox input add rows ??:
not working means value is not set to 10
What value is not set to 10? You can see what it sets:
ui.spinBox->setValue(10);
I would bet my bottom $ that it does exactly that.
and when i setvalue as 10 , 10 rows should be there in my table
Your code has
model2->setRowCount(rows2);
so that is what it does.
also when user change the spinbox value to 11 or 8 no. of rows in table should update
Which spinbox? Only if you call the code to set the rows when the spin value changes, and only if the code is correct. Which i have to say does not look like the case for the 4 lines of code you have shown...
-
code:
tool::tool(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); model = new QStandardItemModel(0, 13, this);//row*col model2 = new QStandardItemModel(0, 19, this) ui.spinBox->setValue(10); ui.spinBox_2->setValue(10);
using this in spinbox value is 10 but the same no. of rows is not updated in my table
so when i set value in spinbox same no. of rows should be added in my table
and when user change value in spinbox row should update (that is happening)connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTableRows); connect(ui.spinBox_2, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTable2Rows); } void tool::setTableRows() { QAbstractItemModel* table1 = ui.tableView->model(); int rows = ui.spinBox->value(); model->setRowCount(rows); } void tool::setTable2Rows() { QAbstractItemModel* table2 = ui.tableView_2->model(); int rows2 = ui.spinBox_2->value(); model2->setRowCount(rows2); }
-
@n-2204 said in spinbox input add rows ??:
using this in spinbox value is 10 but the same no. of rows is not updated in my table
If I understand correctly.
At the time you call
ui.spinBox->setValue(10);
you have not yet done the
connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTableRows);
So if you are expecting the slot
setTableRows()
to be called to actually create rows during thesetValue()
, it won't be. Why should it?Either connect the signals before setting the values, or call the slots explicitly first time before you start out.
Is that what you mean?
-
@JonB said in spinbox input add rows ??:
@n-2204 said in spinbox input add rows ??:
using this in spinbox value is 10 but the same no. of rows is not updated in my table
If I understand correctly.
At the time you call
ui.spinBox->setValue(10);
you have not yet done the
connect(ui.spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &tool::setTableRows);
So if you are expecting the slot
setTableRows()
to be called to actually create rows during thesetValue()
, it won't be. Why should it?Either connect the signals before setting the values, or call the slots explicitly first time before you start out.
ok understood
Is that what you mean?
yes
thanks