Solved
-
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.
-
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
-
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
-
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.
-
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
- 5
- 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_OBJECTpublic:
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();
}
@ -
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.
-
Try this:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtGui/QMainWindow>
namespace Ui
{
class MainWindow;
}
class QTimer;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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));
}@ -
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. -
[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 ;) -
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
-
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_OBJECTpublic:
MainWindow(QWidget *parent = 0); ~MainWindow();
private:
Ui::MainWindow *ui;private slots:
private slots:
};
class mywidget : public QWidget // class mywidget
{
Q_OBJECTpublic:
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