Solved Object of QTableWidget
-
Hi
I write this code :
and i created a object of QTableWidget for public in mainwindows1.hvoid MainWindow::on_pushButton_2_clicked() { table.setCurrentCell(rowname , 1) = ui->textEdit->toPlainText(); rowname = rowname++ ; }
and i get this error :
C:\Users\armin\Documents\DataBase\mainwindow1.cpp:18: error: '((MainWindow*)this)->MainWindow::table' does not have class type
table.setCurrentCell(rowname , 1) = ui->textEdit->toPlainText();
^Why i don't any access to my object of QTableWidget ?
Thanks -
setCurrentCell returns void. Therefore, you cannot assign anything.
-
Hi
Access to the items is via
http://doc.qt.io/qt-5/qtablewidget.html#itemtable.setCurrentCell(rowname , 1) = ui->textEdit->toPlainText();
---->
QString text=ui->textEdit->toPlainText();
QTableWidgetItem *item=table.item(rowname , 1);
if (item)
item->setText(text);
else
qDebug() << "check rowname!!"; -
@mrjj said in Object of QTableWidget:
QTableWidgetItem *item=table.item(rowname , 1);
Thanks.
But i don't want create object in this function.
In fact i create function in header file and public class. -
@koahnig
Thanks. so what i do? -
QString text=ui->textEdit->toPlainText(); // you create a string object here QTableWidgetItem *item=table.item(rowname , 1); // you do not create an object here, you basically store the pointer if (item) item->setText(text); else qDebug() << "check rowname!!";
This would lok more compact, but laso kind of hard to read.
if (table.item(rowname , 1)) table.item(rowname , 1)->setText( ui->textEdit->toPlainText() ); else qDebug() << "check rowname!!";
IMHO it is better to use the initial approach proposed by @mrjj . It is clearer for understanding what is happening. Performancewise both versions are probably at the day's end identical assuming that compiler will end up in the same optimization.
-
I just stumbled over your statement in the first post.
rowname = rowname++ ;
I think in most cases it does not what you are expecting. I was not sure and wrote a small test application:
#include <QCoreApplication> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int rowname = 1; rowname = rowname++; qDebug() << rowname; rowname = rowname++; qDebug() << rowname; return a.exec(); }
The result is:
1 1
Tested on windows 10 with MiinGW Qt 5.4.2 in debug mode.
-
@koahnig said in Object of QTableWidget:
initial
Thanks
So Can't i access to member of object in class from function ? like this:class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); int row = 1; int clm = 7; QTableWidget *table (int row,int clm); int rowname = 1; private slots: void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; };
#include "mainwindow1.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_2_clicked() { table.setCurrentCell(rowname , 1) = ui->textEdit->toPlainText(); rowname = rowname++ ; }
-
You can access a member in an object, but you are introducing a couple of problems in your code. I doubt that you get it compiled at all.
int row = 1; // no possible this way int clm = 7; // see above QTableWidget *table (int row,int clm); // that is a declaration of a function called table int rowname = 1; // same as first problem.
class MainWindow : public QMainWindow { Q_OBJECT int row; int clm; QTableWidget *table; int rowname ; public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; };
#include "mainwindow1.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); row = 1; clm = 7; table = new QTableWidget (int row,int clm); rowname = 1; } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_2_clicked() { QString text=ui->textEdit->toPlainText(); // you create a string object here QTableWidgetItem *item=table.item(rowname , 1); // you do not create an object here, you basically store the pointer if (item) item->setText(text); else qDebug() << "check rowname!!"; ++rowname ; }
Note: this may work. It is not tested at all. Just brain to keyboard.
A friendly recommendation: I would propose to go through some tutorials in C++. You do not do yourself a favor by too much trial and error. You need to get at least a basic understanding there.
-
@koahnig said in Object of QTableWidget:
int row = 1; // no possible this way
It is possible in C++11 but I did not find 1 person yet that thinks it's a good idea. I guess it's acceptable only if you have a lot of constructors and they all must initialise the member to the same value
@koahnig said in Object of QTableWidget:
A friendly recommendation: I would propose to go through some tutorials in C++. You do not do yourself a favor by too much trial and error. You need to get at least a basic understanding there.
I couldn't agree more
-
Thanks for adding this comment. I was not completely aware of this detail. Now after your comment there seem to be a faint memory. ;)
For simple variables this is acceptable, but I am not sure either, if it is a good idea. Latest with the assignment of the table, there should be a problem, because you would need a call to the new operator. Or am I wrong there as well?
-
Thanks all
My problem is solved