HELP! I want to zoomIn and zoomOut scribble area.



  • I am trying to zoomIn And zoomOut the scribble area or paint area..by the following code ,On opening the image and painting on it works fine,but when i try to zoom (view->zoomIn25%) i can zoom only the image,what i had scribbled gets disappeared..why can't i draw on widget? Please help me with the CODE to zoom the scribbleArea along with the image!!

    I have
    main.cpp
    mainwindow.cpp
    scribblearea.cpp along with the header files.

    MAINWINDOW.CPP

    @MainWindow::MainWindow()
    {
    imageLabel = new QLabel;
    scribbleArea = new ScribbleArea;
    setCentralWidget(scribbleArea);
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    scrollArea = new QScrollArea;
    

    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(imageLabel);

    createActions();
    createMenus();
    
    setWindowTitle(tr("Scribble"));
    resize(500, 400);
    

    }

    void MainWindow::open()
    {

    if (maybeSave()) {
    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), QDir::currentPath());
    if (!fileName.isEmpty()) {
    scribbleArea->openImage(fileName);
    QImage image(fileName);
    imageLabel->setPixmap(QPixmap::fromImage(image));

        scaleFactor = 1.0;
    
        printAct->setEnabled(true);
        fitToWindowAct->setEnabled(true);
        updateActions();
    
        if (!fitToWindowAct->isChecked())
            imageLabel->adjustSize();
    }
    
    }
    

    }
    void MainWindow::zoomIn()

    {

    scaleImage(1.25);//"got these from":http://qt-project.org/doc/qt-4.8/widgets-imageviewer-imageviewer-cpp.html
    

    }
    void MainWindow::zoomOut()

    {
    scaleImage(0.8);
    }
    void MainWindow::createActions()

    {

    }
    void MainWindow::createMenus()
    //! [15] //! [16]
    {

    --
    }
    void MainWindow::scaleImage(double factor)

    {
    setCentralWidget(scrollArea);
    //setCentralWidget(scribbleArea);
    Q_ASSERT(imageLabel->pixmap());
    scaleFactor *= factor;
    imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    

    adjustScrollBar(scrollArea->verticalScrollBar(), factor);

    zoomInAct->setEnabled(scaleFactor < 3.0);
    zoomOutAct->setEnabled(scaleFactor > 0.333);
    

    }
    void MainWindow::adjustScrollBar(QScrollBar *scrollBar, double factor)
    //! [25] //! [26]
    {

    }
    void MainWindow::fitToWindow()
    {

     }
     updateActions();
    

    }
    void MainWindow::normalSize()
    {

    }
    void MainWindow::updateActions()
    {

    }@

    SCRIBBLEAREA.CPP
    "have made no changes to this...":http://http://qt-project.org/doc/qt-4.8/widgets-scribble-scribblearea-cpp.html



  • Hi there,

    first: try to use this in a QGraphicsview. It's a little bit easier, and, you can zoom the complete view with all layers in the QGraphicsView.

    For this, you have to create a QGraphicsScene, where you can put a lot of QGraphicItems (doesn't matter if pictures or self created items). And than you can show it in the view.

    second: you don't have to use it, like in the example. Use "scale()", when you use the GraphicsView.

    third: Use fractional numbers for the scaling values. It's more accurate.



  • i was trying to add zoomIn and zoomOut to Qt diagramscene example ..

    added
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
    }
    void MainWindow::mouseReleaseEvent(QMouseEvent *event)
    {
    }
    void MainWindow::paintEvent(QPaintEvent *event)
    {
    }
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    }
    void MainWindow::resizeImage(QImage *image, const QSize &newSize)
    {
    }
    void MainWindow::drawLineTo(const QPoint &endPoint)
    {
    }

    Still No errors nor No desired output... How to draw/paint in that already present drag and drop area.

    I am implementing a paint application for now i want file->open image and
    scribble on that and Zoom the complete thing.



  • Is there any code implemented?



  • the complete diagramscene example here-

    http://harmattan-dev.nokia.com/docs/library/html/qt4/graphicsview-diagramscene.html

    i tried inserting the below mentioned functions into "mainwindow.cpp" of diagramscene ..
    @
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton) {
    lastPoint = event->pos();
    scribbling = true;
    }
    }

    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
    if ((event->buttons() & Qt::LeftButton) && scribbling)
    drawLineTo(event->pos());
    }

    void ScribbleArea::mouseReleaseEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton && scribbling) {
    drawLineTo(event->pos());
    scribbling = false;
    }
    }

    void MainWindow::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    QRect dirtyRect = event->rect();
    painter.drawImage(dirtyRect, image, dirtyRect);
    }

    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    if (width() > image.width() || height() > image.height()) {
    int newWidth = qMax(width() + 128, image.width());
    int newHeight = qMax(height() + 128, image.height());
    resizeImage(&image, QSize(newWidth, newHeight));
    update();
    }
    QWidget::resizeEvent(event);
    }

    void MainWindow::drawLineTo(const QPoint &endPoint)
    {
    QPainter painter(&image);
    painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
    Qt::RoundJoin));
    painter.drawLine(lastPoint, endPoint);
    modified = true;

     int rad = (myPenWidth / 2) + 2;
     update(QRect(lastPoint, endPoint).normalized()
                                      .adjusted(-rad, -rad, +rad, +rad));
     lastPoint = endPoint;
    

    }

    void MainWindow::resizeImage(QImage *image, const QSize &newSize)
    {
    if (image->size() == newSize)
    return;

     QImage newImage(newSize, QImage::Format_RGB32);
     newImage.fill(qRgb(255, 255, 255));
     QPainter painter(&newImage);
     painter.drawImage(QPoint(0, 0), *image);
     *image = newImage;
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.