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

Solved

Scheduled Pinned Locked Moved General and Desktop
19 Posts 3 Posters 5.9k Views 1 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.
  • S Offline
    S Offline
    sidharth
    wrote on last edited by
    #1

    i have a Qtablewidget in forms and i want to add data dynamically in the rows and columns.i have a timer and i am incrementing value of a variable after every 1 second. so i want that after everyone second my value should generate in table and when after 1 second my second value comes ,my first value should shift in second row and so on...
    lyk this:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGroupbox>
    #include <QPainter>
    #include <QPen>
    #include <QTimer>
    #include <QTime>
    int r=0;
    int c=0;
    int b=0;
    QString textstr;
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

     QTimer *timer = new QTimer(this);
    
      timer->start(1000);
    
    
     connect(timer, SIGNAL(timeout()), this, SLOT(time()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow :: paintEvent(QPaintEvent *)
    {

     QVariant v( b);
    
      textstr=v.toString();
    
     //QPainter p(this);
    
     // p.drawText(10,50,textstr);
    

    for(r=0;r<1;r++)
    {
    for(c=0;c<1;c++)
    {

           QTableWidgetItem *item = new QTableWidgetItem();
            item->setText(textstr);
            ui->tableWidget->setItem(r,c,item);
        }
    

    }

    }

    void MainWindow :: time()
    {
    b++;

    update();

    }@

    i want that everytime my new value comes ,a new row is created.

    so please help me out by changing this code.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi sidharth!
      First of all use code tags "@" for posting code parts.
      For adding rows to QTableWidget you not need use paintEvent just do it in time() slot
      @ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);

      QTableWidgetItem* item = new QTableWidgetItem;
      item->setText(QString::number(b));
      ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 0, item);@
      

      look at "QTableWidget":http://qt-project.org/doc/qt-5.0/qtwidgets/qtablewidget.html

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        However, i'd recommend you use QTableView with QStandardItemModel instead QTableWidget.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sidharth
          wrote on last edited by
          #4

          thankyou sir,
          i got your code but it is generating rows and data but it is generating in a order 0123..........
          i want that if my first value is 1 in row1 then when my second value ie 2 comes my first value gets shift to row 2 and the when my third value ie 3 comes my previous value shifts in lower row...
          i mean to say everytime my new value should appear in row1...
          plz help me out with edition in the same code...

          thankyou once again

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            Ok, try this:
            @ui->tableWidget->insertRow(0);
            QTableWidgetItem* item = new QTableWidgetItem;
            item->setText(QString::number(b));
            ui->tableWidget->setItem(0, 0, item);@

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sidharth
              wrote on last edited by
              #6

              thanks alot sir..ur code worked for me.but my format of generating data is like it is generating every number many times...

              2
              2
              2
              2
              1
              1
              1
              1
              1
              0
              0
              0
              0
              0

              like this .why it is hapening can you tell me sir..?

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #7

                Because you have two loops one inside another.
                @for(r=0;r<1;r++) {
                for(c=0;c<1;c++) {
                QTableWidgetItem *item = new QTableWidgetItem();
                item->setText(textstr);
                ui->tableWidget->setItem(r,c,item);
                }
                }@

                Can you show what you expect at output? Then i can direct you to right way.

                ps - and please edit your firs post, add code tags.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sidharth
                  wrote on last edited by
                  #8

                  i added tags and thanx for telling me.
                  after your first reply i deleted my loops and right now i am not using any for loops.

                  i exactly want like this
                  i am drawing my desired output table.

                   value
                  
                  1. 5
                  2. 4
                    3 3
                    4 2
                    5 1
                    6 0

                  like this i want to update my every new value in row1 and previous values will slide down.
                  i am sending you my whole code
                  .header
                  @
                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H

                  #include <QtGui/QMainWindow>

                  namespace Ui
                  {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

                  public:
                  void paintEvent(QPaintEvent *);

                  MainWindow(QWidget *parent = 0);
                  ~MainWindow();
                  

                  private:
                  Ui::MainWindow *ui;
                  private slots:
                  void time();
                  };

                  #endif // MAINWINDOW_H
                  @

                  .cpp

                  @
                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QGroupbox>
                  #include <QPainter>
                  #include <QPen>
                  #include <QTimer>
                  #include <QTime>
                  int r=0;
                  int c=0;
                  int b=0;
                  QString textstr;
                  MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent), ui(new Ui::MainWindow)
                  {
                  ui->setupUi(this);

                   QTimer *timer = new QTimer(this);
                  
                    timer->start(1000);
                  
                  
                   connect(timer, SIGNAL(timeout()), this, SLOT(time()));
                  

                  }

                  MainWindow::~MainWindow()
                  {
                  delete ui;
                  }
                  void MainWindow :: paintEvent(QPaintEvent *)
                  {

                  ui->tableWidget->insertRow(0);
                  QTableWidgetItem* item = new QTableWidgetItem;
                  item->setText(QString::number(b));
                  ui->tableWidget->setItem(0, 0, item);

                  item->setForeground(QColor::fromRgb(0,128,0));

                  }

                  void MainWindow :: time()
                  {

                  b++;

                  update();

                  }
                  @

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sidharth
                    wrote on last edited by
                    #9

                    mean to say like for row1 value5,then row 2 value 4 ..my column name is value..like this everytime my new value comes it will be in row 1 and previous will be sliding down the rows.

                    you can check my code what i tried and please tell me the solution .you have already done 90% .just a small issue is coming that my every value is generating many times.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #10

                      Try this:
                      @#ifndef MAINWINDOW_H
                      #define MAINWINDOW_H

                      #include <QtGui/QMainWindow>

                      namespace Ui
                      {
                      class MainWindow;
                      }
                      class QTimer;
                      class MainWindow : public QMainWindow
                      {
                      Q_OBJECT

                      public:
                      MainWindow(QWidget *parent = 0);
                      ~MainWindow();

                      private:
                      Ui::MainWindow *ui;
                      int b;
                      QTimer *timer
                      private slots:
                      void on_timeout();

                      };

                      #endif // MAINWINDOW_H@

                      @#include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include <QTimer>

                      MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent), ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);
                      b = 0;
                      timer = new QTimer(this);
                      timer->start(1000);
                      connect(timer, SIGNAL(timeout()), this, SLOT(on_timeout()));
                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      void MainWindow :: on_timeout()
                      {
                      b++;
                      ui->tableWidget->insertRow(0);
                      QTableWidgetItem* item = new QTableWidgetItem;
                      item->setText(QString::number(b));
                      ui->tableWidget->setItem(0, 0, item);
                      item->setForeground(QColor::fromRgb(0,128,0));
                      }@

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sidharth
                        wrote on last edited by
                        #11

                        thanx a lot sir for giving me your valuable time.you finally did it what i wanted.
                        one more question i have to ask.
                        can you tell me what was wrong with the previous code and what exactly insertrow(0); do..?

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          Generally what i did, is remove paintEvent. As i wrote before "post":https://qt-project.org/forums/viewreply/128236/ and few other changes, you can see it by comparing code texts.
                          About insert row you can read in "doc":http://qt-project.org/doc/qt-5.0/qtwidgets/qtablewidget.html it just adds an empty row into the table at given position, if there was a row, existing row moves down.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sidharth
                            wrote on last edited by
                            #13

                            ok sir,got it .thankyou again for your help.give me your mail address also please.

                            1 Reply Last reply
                            0
                            • Q Offline
                              Q Offline
                              qxoz
                              wrote on last edited by
                              #14

                              You're welcome.
                              Please mark thread as solved (edit first post and add to title [SOLVED] text).
                              You can send messages to mail address of members from this site. Just go to a members profile, there you will find send email link.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                sidharth
                                wrote on last edited by
                                #15

                                sir please tell me that if i want to generate this data in all of my 4 columns then i am applying for loop
                                @for(c=0;c<4;c++)@ and then @setItem(0,c,item)@.
                                but its not working what is the problem i am facing..?

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  sidharth
                                  wrote on last edited by
                                  #16

                                  sir i got the solution..it was @for(c=0;c<0;c=c+1)@

                                  1 Reply Last reply
                                  0
                                  • raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    [quote author="sidharth" date="1370587123"]sir i got the solution..it was @for(c=0;c<0;c=c+1)@[/quote]
                                    i doubt that the contents of this loop are ever executed ;)

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      sidharth
                                      wrote on last edited by
                                      #18

                                      yes sir you are right... :)

                                      can you help me further ?.now i have stuck in some other case.
                                      i have pushbutton in forms created in base class mainwidow .now i have created one widget on the same form by drag and drop and again i created a pushbutton on the class widget.now i want that on the click of my pushbutton which was on the widget,my mainwindow button got disappeared...

                                      how can it be possible.?please help me out

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        sidharth
                                        wrote on last edited by
                                        #19

                                        i have promoted the widget to class mywidget and done some thing like this

                                        .h
                                        @#ifndef MAINWINDOW_H
                                        #define MAINWINDOW_H

                                        #include <QtGui/QMainWindow>

                                        namespace Ui
                                        {
                                        class MainWindow;
                                        }

                                        class MainWindow : public QMainWindow //class mainwindow
                                        {
                                        Q_OBJECT

                                        public:

                                        MainWindow(QWidget *parent = 0);
                                        ~MainWindow();
                                        

                                        private:
                                        Ui::MainWindow *ui;

                                        private slots:

                                        private slots:

                                        };

                                        class mywidget : public QWidget // class mywidget

                                        {
                                        Q_OBJECT

                                        public:

                                        mywidget(QWidget *parent = 0);

                                        public slots:

                                        private:
                                        Ui::MainWindow *ui;

                                        private slots:
                                        

                                        void on_pushButton_2_clicked();
                                        };
                                        #endif // MAINWINDOW_H@

                                        .cpp

                                        @
                                        #include "mainwindow.h"
                                        #include "ui_mainwindow.h"

                                        MainWindow::MainWindow(QWidget *parent)
                                        : QMainWindow(parent), ui(new Ui::MainWindow)
                                        {
                                        ui->setupUi(this);

                                        }

                                        MainWindow::~MainWindow()
                                        {
                                        delete ui;
                                        }
                                        mywidget::mywidget(QWidget *parent)
                                        : QWidget(parent)

                                        {

                                        }

                                        void mywidget::on_pushButton_2_clicked()
                                        {

                                        }@

                                        tell me how to do it sir ..
                                        please

                                        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