Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Object of QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Object of QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 3.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Armin

    Hi
    I write this code :
    and i created a object of QTableWidget for public in mainwindows1.h

    void 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

    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @Armin

    setCurrentCell returns void. Therefore, you cannot assign anything.

    Vote the answer(s) that helped you to solve your issue(s)

    A 1 Reply Last reply
    3
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi
      Access to the items is via
      http://doc.qt.io/qt-5/qtablewidget.html#item

      table.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!!";

      http://doc.qt.io/qt-5/qtablewidgetitem.html#setText

      A 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Access to the items is via
        http://doc.qt.io/qt-5/qtablewidget.html#item

        table.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!!";

        http://doc.qt.io/qt-5/qtablewidgetitem.html#setText

        A Offline
        A Offline
        Armin
        wrote on last edited by
        #4

        @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.

        1 Reply Last reply
        0
        • K koahnig

          @Armin

          setCurrentCell returns void. Therefore, you cannot assign anything.

          A Offline
          A Offline
          Armin
          wrote on last edited by
          #5

          @koahnig
          Thanks. so what i do?

          K 2 Replies Last reply
          0
          • A Armin

            @koahnig
            Thanks. so what i do?

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #6

            @Armin

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            A 1 Reply Last reply
            4
            • A Armin

              @koahnig
              Thanks. so what i do?

              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              @Armin

              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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              2
              • K koahnig

                @Armin

                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.

                A Offline
                A Offline
                Armin
                wrote on last edited by
                #8

                @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++ ;
                }
                
                
                K 1 Reply Last reply
                0
                • A Armin

                  @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++ ;
                  }
                  
                  
                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #9

                  @Armin

                  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.

                  Vote the answer(s) that helped you to solve your issue(s)

                  VRoninV 1 Reply Last reply
                  2
                  • K koahnig

                    @Armin

                    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.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #10

                    @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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    K 1 Reply Last reply
                    2
                    • VRoninV VRonin

                      @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

                      K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #11

                      @VRonin

                      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?

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Armin
                        wrote on last edited by
                        #12

                        Thanks all
                        My problem is solved

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved