Print db file displayed in tableView - qt

Is it possible to add print option to print the db contents in tableView of Qt?
ie, I have a test.db displayed in tableView. I want to add an option to print the database. Is it possible?

If you mean printing with a printer, it can be basicly done like this:
int width = 0;
int height = 0;
tableView->resizeColumnsToContents();
const int columnCnt = tableView->model()->columnCount();
for( int i = 0; i < columnCnt; ++i )
{
width += pTableView->columnWidth(i);
}
const int rowCnt = tableView->model()->rowCount();
for( int i = 0; i < rowCnt; ++i )
{
height += tableView->rowHeight(i);
}
tableView->setFixedSize(width, height);
QPrinter printer;
// ... printer settings ...
tableView->render(&printer);
You can find more about this topic here:
http://blog.qt.io/blog/2012/08/24/qt-commercial-support-weekly-25-printing-large-tables-2/

I used the code in the below link
http://www.qtcentre.org/archive/index.php/t-64182.html
but when I call the function using a QPushButton, the application freezes and get closed.
What's the error in the code?

Related

Why does it give an error? ui->graphicsView

My goal is to print a QImage which consists of random RGB values. However, I got this error:
No member named 'graphicsView' in 'Ui::MainWindow'
My code is here:
int sizeX = 300; int sizeY = 300;
QImage img = QImage(sizeX, sizeY, QImage::Format_RGB32);
for(int i=0; i<sizeX; i++){
for(int j=0; j<sizeY; j++){
img.setPixel(i, j, qRgb(rand()%256, rand()%256, rand()%256));
}
}
QGraphicsScene *graphic = new QGraphicsScene(this);
graphic->addPixmap(QPixmap::fromImage(img));
ui->graphicsView->setScene(graphic);
m_ig = new ImageGenerator;
connect(m_ig, &ImageGenerator::sigTest, this, &MainWindow::slotTest);
Thanks. Best regards!
Make sure that Graphics View widget is added to your MainWindow form (usually mainwindow.ui file) in Design section of QtCreator.
Check if its objectName is actually graphicsView.
Try to rebuild your project.

QLabel won't update pixmap from inside function

I've tried absolutely everything I know, and I've come to the conclusion that this issue is over my head. I've tried running repaint(), update() and this->update(); and everything else that I could think of. Pixmap works outside of the function (in the constructor) but not inside a function. Here is code (only relevant is pasted, please indicate if you would like more):
myWidget.h
#define NUM_POINTERS 10
QLabel* pointerArray[NUM_POINTERS];
QPixmap circle;
QPixmap* triangle;
QPixmap* whitex;
int activePointer;
myWidget.cpp
activePointer = 0;
QPixmap circle (":/Resources/greencircle.png");
this->whitex = new QPixmap(":/Resources/white_x.png");
this->triangle = new QPixmap(":/Resources/redtriangle.png");
//create an array of pointers to the label1-10 objects
pointerArray[0] = ui->label1;
pointerArray[1] = ui->label2;
pointerArray[2] = ui->label3;
...
pointerArray[9] = ui->label10;
for (int i = 0;i < 10; i++)
{
pointerArray[i]->setPixmap(circle);
}
void myWidget::changeImage()
{
updatesEnabled();
if (activePointer < 10){
pointerArray[activePointer]->setPixmap(*this->whitex);
activePointer++;
update();
}
else{
printf("end of array\n");
fflush(stdout);
}
}
I get a row of circles printed where I want them, but I won't get any white Xs. The pixmap changes to the whitex, but it will not update. It does not crash, it continues adding to activePointer until the end of the array.
Thanks in advance.
Quick Edit: I have tried pointerArray[activePointer]->update(); with no luck.

Resize window when showing hidden widget in Qt

I have a hidden widget in my dialog. When I show it, I want the dialog window to expand accordingly and shrink again when I choose to hide it.
How can this be done? I have tried understanding the layout functionality of Qt but I find it very hard to grasp.
Try use QWidget::adjustSize() for container after show/hide content.
Not sure if there is a build-in solution, but this is my manual one:
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
connect(toolButton, SIGNAL(toggled(bool)), SLOT(onToolButton(bool)));
onToolButton(false);
}
void Dialog::onToolButton(bool checked)
{
lineEdit->setVisible(checked);
int maxHeight = verticalLayout->contentsMargins().top()
+ verticalLayout->contentsMargins().bottom();
int itemsCount = 0;
for (int i = 0; i < verticalLayout->count(); ++i) {
QLayoutItem *item = verticalLayout->itemAt(i);
if (item->widget()) {
QWidget *w = item->widget();
if (w->isVisible()) {
maxHeight += w->geometry().height();
++itemsCount;
}
} else if (item->layout()) {
QLayout *l = item->layout();
maxHeight += l->geometry().height();
++itemsCount;
}
}
if (itemsCount > 1)
maxHeight += ((itemsCount - 1) * verticalLayout->spacing());
setMaximumHeight(maxHeight);
}

Populating Table Widget from Text File in Qt

I'm new to Qt and need some help with the following:
I would like to create a GUI containing a Table Widget that is populated by information coming from a tab delimited text file. In my GUI, the user would first browse for the text file and then it would then show the contents in the Table Widget. I've done the browse part, but how do I load the data from the text file into the Table Widget?
It's two steps, parse the file, and then push it into the widget.
I grabbed these lines from the QFile documentation.
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}
Your process_line function should look like this:
static int row = 0;
QStringList ss = line.split('\t');
if(ui->tableWidget->rowCount() < row + 1)
ui->tableWidget->setRowCount(row + 1);
if(ui->tableWidget->columnCount() < ss.size())
ui->tableWidget->setColumnCount( ss.size() );
for( int column = 0; column < ss.size(); column++)
{
QTableWidgetItem *newItem = new QTableWidgetItem( ss.at(column) );
ui->tableWidget->setItem(row, column, newItem);
}
row++;
For more information about manipulating QTableWidgets, check the documentation. For new users using the GUI builder in Qt Creator, it is tricky figuring it out at first.
Eventually I would recommend to switching to building the GUI the way they do in all their examples... by adding everything by hand in the code instead of dragging and dropping.
Sorry...
void squidlogreader_::process_line(QString line)
{
static int row = 0;
QStringList ss = line.split('\t');
if(ui->tableWidget->rowCount() < row + 1)
ui->tableWidget->setRowCount(row + 1);
if(ui->tableWidget->columnCount() < ss.size())
ui->tableWidget->setColumnCount( ss.size() );
for( int column = 0; column < ss.size(); column++)
{
QTableWidgetItem *newItem = new QTableWidgetItem( ss.at(column) );
ui->tableWidget->setItem(row, column, newItem);
}
row++;
}
void squidlogreader_::on_pushButton_clicked()
{
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QString line = file.readLine();
process_line(line);
}

QT resize problem after zoom

I'm trying to zoom in a picture using QT and C++.
I inherited QLabel objects in my classes to show pictures. And also put this QLabels in mdiarea.
zoom function is working fine however the qlabel does not update its size immediately. If I try to resize is manually (with cursor) program automatically handles it and resize the qlabel as I wish.
How can I update the sizes immediately.
Thanks for help. :)
bool MdiChild::event ( QEvent * e ){
//qDebug("asd1");
if(e->type() == QEvent::Wheel){
int numDegrees = ((QWheelEvent*) e)->delta() / 8;
double numSteps = (double)numDegrees/150;
int w = pix->width();
int h = pix->height();
ratio += numSteps;
qDebug("ratio = %f", ratio);
QPixmap* p = new QPixmap(pix->scaledToHeight ( (int)(h * ratio),Qt::FastTransformation ));
setPixmap(*p);
adjustSize();
adjustSize();
update();
}
return QWidget::event(e);
}
The problem is resolved but I think I cannot answer my own question.
When I add the same event to the parent window problem is solved. But when I maximize the window the inner object also got the event and crashes the maximized window.
bool ImageProcessor::event ( QEvent * e ){
if(e->type() == QEvent::Wheel){
QList<QMdiSubWindow *> childList = ui.mdiArea->subWindowList();
for(int i = 0; i<childList.count(); i++){
childList[i]->adjustSize();
}
}
return QWidget::event(e);
}
You need a QScrollArea to hold your QLabel. Otherwise when the window resizes your QLabel will not have scrollbars.
Look at the examples to see how to create an image viewer and how they resize.
ImageViewer example
Zoomable Picture Viewer
The problem is resolved but I think I cannot answer my own question. When I add the same event to the parent window problem is solved. But when I maximize the window the inner object also got the event and crashes the maximized window.
bool ImageProcessor::event ( QEvent * e ){
if(e->type() == QEvent::Wheel){
QList<QMdiSubWindow *> childList = ui.mdiArea->subWindowList();
for(int i = 0; i<childList.count(); i++){
childList[i]->adjustSize();
}
}
return QWidget::event(e);
}

Resources