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.
Related
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?
I want to display the same image at different position. Image should display at all the position. I am using Qpixmap for placing image. Can any one suggest how to do it?.
You can use QLabel for this:
QList<QPoint> points = QList<QPoint>()
<< QPoint(0, 0)
<< QPoint(100, 100)
<< QPoint(200, 200);
QPixmap pixmap;
for (int i = 0; i < points.size(); ++i)
{
QLabel* label = new QLabel;
label->setPixmap(pixmap);
label->setGeometry(QRect(points[i], pixmap.size()));
label->show();
}
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.
I'm trying to create an application based on model/view concept.
i need to open some directory, find all imgs in it and show them in MainWindow (subclass of QMainWindow).
The architecture is something like this:
1) via QDir create QStringList of "good" file names (using file names filter by extentions).
2) create QStandardItemModel and fill it with QStandardItem (QIcon(QImage(fileName).scaled(QSize)), fileName).
3) use QListView to show data from the model.
but there is some problems.
first of all - theModel.columnCount is, e.g., 52 but only one picture is shown on the screen and without its name.
can someone help me:
1) how to fill model correctly? my approach:
QDir dirs(dir);
QStringList imgs = dirs.entryList(QStringList() << "*.jpg" << "*.jpeg" << "*.bmp" << "*.png");
itemModel->clear();
QList<QStandardItem *> listItem;
for(int i = 0; i < imgs.count(); ++i){
QImage image = QImage(dir + "/" + imgs.at(i)).scaled(QSize(size().width()/4, size().height()/4));
QStandardItem *item = new QStandardItem();
item->setIcon(QIcon(QPixmap::fromImage(image)));
item->setData(imgs.at(i));
listItem << item;
}
itemModel->appendRow(listItem);
this code is in one slot of the MainWindow class.
2) as I understand, my view is automatically updated, so it should show all data from the model.
am I right, or some code is necessary?
3) maybe I haven't done somethings in initialization of the model and the view (the code is in te constructor of class MainWindow):
itemModel = new QStandardItemModel(this);
listView = new QListView(this);
listView->setModel(itemModel);
// listView->setFlow(QListView::LeftToRight);
// listView->setLayoutMode(QListView::Batched);
listView->setViewMode(QListView::IconMode);
listView->setResizeMode(QListView::Adjust);
// listView->setGridSize(QSize(size().width()/4, size().height()/4));
listView->setIconSize(QSize(size().width()/4, size().height()/4));
setCentralWidget(listView);
Since you determined that you needed appendColumn the last bit would be to add the QIcon as data with the Qt::DecorationRole. The follow works for me for viewing images in the same folder the program is run (though I don't know why it is shown with a grid layout).
#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QDir>
#include <QStringList>
#include <QList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel* itemModel = new QStandardItemModel();
QListView* listView = new QListView();
QDir dirs(".");
QStringList imgs = dirs.entryList(QStringList() << "*.jpg" << "*.jpeg" << "*.bmp" << "*.png");
QList<QStandardItem *> listItem;
for(int i = 0; i < imgs.count(); ++i){
QImage image = QImage(dirs.absoluteFilePath(imgs.at(i))).scaled(QSize(80, 60));
QStandardItem *item = new QStandardItem();
item->setData(QVariant(QPixmap::fromImage(image)), Qt::DecorationRole);
listItem << item;
}
itemModel->appendColumn(listItem);
listView->setModel(itemModel);
listView->setViewMode(QListView::IconMode);
listView->show();
a.exec();
}
I have a list of objects that I use to add objects into a QGraphicsScene:
for(int i = 0; i < levelObjects.length(); i++)
{
QRect objRect;
objRect = spriteSheetLocations.value(levelObjects.at(i).value("frame_name"));
//Q_ASSERT_X(objRect != QRect(0,0,0,0), "MainWindow::loadFile()", "Could not find sprite location!");
QImage img = spriteSheet.copy(objRect);
int height = levelObjects.at(i).value("height").toInt();
int width = levelObjects.at(i).value("width").toInt();
int x = levelObjects.at(i).value("x").toInt();
int y = levelObjects.at(i).value("y").toInt();
img = img.scaled(QSize(width, height), Qt::IgnoreAspectRatio);
item = scene->addPixmap(QPixmap::fromImage(img));
int xPos = x - width/2;
int yPos = levelPlist.value("level_height").toInt() - (y + height/2);
item->setPos(xPos, yPos);
}
Later on, in the GraphicsScene class, I detect when the user clicks on an item and drags it to move it:
void LevelGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (QGraphicsItem *item = itemAt(event->pos())) {
qDebug() << "You clicked on item" << item;
draggedItem = item;
int mouseX = draggedItem->pos().x() - mapToScene(event->pos()).x();
int mouseY = draggedItem->pos().y() - mapToScene(event->pos()).y();
mouseOffset = QPointF(mouseX, mouseY);
} else {
qDebug() << "You didn't click on an item.";
draggedItem = NULL;
mouseOffset = QPointF(0,0);
}
}
void LevelGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if(!draggedItem) // no item selected
return;
QPointF pos = mapToScene(event->pos()) + mouseOffset;
draggedItem->setPos(pos);
}
This works fine for moving the items in the graphics view, but I'm having trouble tracing the QGraphicsItem back to the list item that created it.
What's the best way to link the QGraphicsItem with the list item from which it was made so that the list item can be changed to reflect the change of position?
You could assign each item in your domain object a QUuid property and pass this along to a property in your QGraphicsItem. I have used this on a project and it works quite well. I added a QHash lookup table to my domain model to make it more efficient, but this would not be necessary for shorter lists.
The best way would be a way that does not require to manualy sync items from your list and items on the scene.
The best way to do that depends on your design - may be your items can become pointers to the items on the scene or they can hold ones.