Rubber band drag mode of graphics view



  • How can I get the selection area after releasing left mouse button after dragging rubber band over a graphics view?

    thank you


  • Moderators

    Hi,

    From Qt 5.1 onwards, QGraphicsView emits the rubberBandChanged(QRect, QPointF, QPointF) signal. The QRect parameter represents the selection area.

    One possible solution is to listen for this signal, and check the value of the QRect:

    • If you get a valid QRect, that means the user is still dragging. Store that QRect in memory.
    • If you get a null QRect, that means the user has just released the mouse button. Retrieve the last non-null QRect -- this is your selection area.


  • This is great, just bear with me:

    I added the following slot to the window class:

     private slots:
         void handleRubberBandChanged(QRect r, QPointF pA, QPointF pB);
    

    Then in the constructor connected the following method to the slot:

    connect(this, SIGNAL(rubberBandChanged(QRect, QPointF, QPointF)), this, SLOT(handleRubberBandChanged(QRect, QPointF, QPointF)));
    

    This is the definition of the method:

    void newForm::handleRubberBandChanged(QRect r, QPointF pA, QPointF pB){
        if (!r.isNull()){
            rect = r;
        }
        else {
            handleButton();
        }
    }
    

    But the method does not get called and after adding the slot Netbeans debugger is going crazy and crashing all the time.

    At runtime it outputs the following:

    Object::connect: No such signal QGraphicsView::rubberBandChanged(QRect, QPointF, QPointF) in newForm.cpp.cc:49
    Object::connect:  (sender name:   'graphicsView')
                                           Object::connect:  (receiver name: 'newForm')
    

    What am I doing wrong? Maybe it is the problem with the includes of this unit?

    #include <qt4/QtGui/qpixmap.h>
    #include <qt5/QtGui/qrgb.h>
    #include <qt4/QtGui/qimage.h>
    #include <qt4/QtGui/qfont.h>
    #include <QtCore>
    #include <QFutureWatcher>
    #include <QMessageBox>
    

    Thank you!


  • Moderators

    @nulluse said:

    At runtime it outputs the following:

    Object::connect: No such signal QGraphicsView::rubberBandChanged(QRect, QPointF, QPointF) in newForm.cpp.cc:49
    

    It looks like your program is being linked against Qt 4 for some reason, which doesn't have that signal.

    #include <qt4/QtGui/qpixmap.h>
    #include <qt5/QtGui/qrgb.h>
    

    This doesn't look right; are you explicitly including both Qt 4 and Qt 5?

    The recommended way to include headers in Qt is simply to #include the class names, e.g.

    #include <QPixmap>
    #include <QRgb>
    

    But the method does not get called and after adding the slot Netbeans debugger is going crazy and crashing all the time.

    I'm afraid I'm not familiar with the Netbeans environment. Can you provide details on how you configured, compiled, and ran your project?

    Are you willing to use Qt Creator instead?



  • I wanted to post the changes to the headers I made to conform to Qt5 but cannot get through the spam filter that thinks something in my #includes is spam. I will try adding them line by line until it triggers the spam filter:

    Netbeans is doing this to me and I do not know yet how to stop it - it adds the headers all by itself when I use the classes in the code.
    The Qt5 headers included here were my changes. But apparently I have not changed them all.

    I left only the following includes in the main.cpp file:

        #include <QApplication>
        #include <QWidget>
    

    and only these in the .cpp.cc file of the main window:

        #include <QPixmap>
        #include <QRgb>
        #include <QtCore>
        #include <QFutureWatcher>
        #include <QMessageBox>
    
        #include "newForm.h"
        #include <QCoreApplication>
    

    but the signal is still not getting fired, but there is no more error in the output at runtime.
    The output of build process is showing qt4 in several places, is that because Netbeans is using qmake-qt4?

    cd '/usr/home/leo/NetBeansProjects/QtApplication_1'
    /usr/local/bin/gmake -f Makefile CONF=Debug clean QMAKE=/usr/local/bin/qmake-qt4
    "/usr/local/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE=/usr/local/bin/qmake-qt4 SUBPROJECTS= .clean-conf
    gmake[1]: Entering directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    /usr/local/bin/qmake-qt4 VPATH=. -o qttmp-Debug.mk nbproject/qt-Debug.pro
    mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
    /usr/local/bin/gmake -f nbproject/qt-Debug.mk distclean
    gmake[2]: Entering directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    rm -f moc_newForm.cpp
    rm -f ui_newForm.h
    rm -f build/Debug/GNU-Generic/main.o build/Debug/GNU-Generic/newForm.cpp.o build/Debug/GNU-Generic/moc_newForm.o
    rm -f *~ core *.core
    rm -f dist/Debug/GNU-Generic/QtApplication_1 
    rm -f qttmp-Debug.mk
    gmake[2]: Leaving directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    gmake[1]: Leaving directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    
    CLEAN SUCCESSFUL (total time: 108ms)
    cd '/usr/home/leo/NetBeansProjects/QtApplication_1'
    /usr/local/bin/gmake -f Makefile CONF=Debug QMAKE=/usr/local/bin/qmake-qt4
    "/usr/local/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE=/usr/local/bin/qmake-qt4 SUBPROJECTS= .build-conf
    gmake[1]: Entering directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    /usr/local/bin/qmake-qt4 VPATH=. -o qttmp-Debug.mk nbproject/qt-Debug.pro
    mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
    "/usr/local/bin/gmake" -f nbproject/qt-Debug.mk dist/Debug/GNU-Generic/QtApplication_1
    gmake[2]: Entering directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    /usr/local/bin/uic-qt4 newForm.ui -o ui_newForm.h
    g++ -c -pipe -std=c++11 -g -Wall -W -pthread -D_THREAD_SAFE -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../../local/share/qt4/mkspecs/freebsd-clang -Inbproject -I../../../../local/include/qt4/QtCore -I../../../../local/include/qt4/QtGui -I../../../../local/include/qt4 -I. -I. -Inbproject -I../../../../local/include/qt4 -I../../../../local/include -I. -o build/Debug/GNU-Generic/main.o main.cpp
    g++ -c -pipe -std=c++11 -g -Wall -W -pthread -D_THREAD_SAFE -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../../local/share/qt4/mkspecs/freebsd-clang -Inbproject -I../../../../local/include/qt4/QtCore -I../../../../local/include/qt4/QtGui -I../../../../local/include/qt4 -I. -I. -Inbproject -I../../../../local/include/qt4 -I../../../../local/include -I. -o build/Debug/GNU-Generic/newForm.cpp.o newForm.cpp.cc
    newForm.cpp.cc:168:6: warning: unused parameter 'pA' [-Wunused-parameter]
     void newForm::handleRubberBandChanged(QRect r, QPointF pA, QPointF pB){
          ^
    newForm.cpp.cc:168:6: warning: unused parameter 'pB' [-Wunused-parameter]
    newForm.cpp.cc: In function 'QString FileErrorMessage(int)':
    newForm.cpp.cc:28:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    /usr/local/bin/moc-qt4 -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../../local/share/qt4/mkspecs/freebsd-clang -Inbproject -I../../../../local/include/qt4/QtCore -I../../../../local/include/qt4/QtGui -I../../../../local/include/qt4 -I. -I. -Inbproject -I../../../../local/include/qt4 -I../../../../local/include -I. newForm.h -o moc_newForm.cpp
    g++ -c -pipe -std=c++11 -g -Wall -W -pthread -D_THREAD_SAFE -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../../local/share/qt4/mkspecs/freebsd-clang -Inbproject -I../../../../local/include/qt4/QtCore -I../../../../local/include/qt4/QtGui -I../../../../local/include/qt4 -I. -I. -Inbproject -I../../../../local/include/qt4 -I../../../../local/include -I. -o build/Debug/GNU-Generic/moc_newForm.o moc_newForm.cpp
    clang++ -pthread -Wl,-rpath,/usr/local/lib/qt4 -o dist/Debug/GNU-Generic/QtApplication_1 build/Debug/GNU-Generic/main.o build/Debug/GNU-Generic/newForm.cpp.o build/Debug/GNU-Generic/moc_newForm.o    -L/usr/local/lib/qt4 -L/usr/local/lib -lQtGui -L/usr/local/lib -L/usr/local/lib/qt4 -lQtCore 
    { test -n "dist/Debug/GNU-Generic/" && DESTDIR="dist/Debug/GNU-Generic/" || DESTDIR=.; } && test $(gdb --version | sed -e 's,[^0-9][^0-9]*\([0-9]\)\.\([0-9]\).*,\1\2,;q') -gt 72 && gdb --nx --batch --quiet -ex 'set confirm off' -ex "save gdb-index $DESTDIR" -ex quit 'dist/Debug/GNU-Generic/QtApplication_1' && test -f dist/Debug/GNU-Generic/QtApplication_1.gdb-index && objcopy --add-section '.gdb_index=dist/Debug/GNU-Generic/QtApplication_1.gdb-index' --set-section-flags '.gdb_index=readonly' 'dist/Debug/GNU-Generic/QtApplication_1' 'dist/Debug/GNU-Generic/QtApplication_1' && rm -f dist/Debug/GNU-Generic/QtApplication_1.gdb-index || true
    gmake[2]: Leaving directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    gmake[1]: Leaving directory '/usr/home/leo/NetBeansProjects/QtApplication_1'
    
    BUILD SUCCESSFUL (total time: 3s)
    

    There seems to be another qmake:

    /usr/local/lib/qt5/bin> ./qmake -v
    QMake version 3.0
    Using Qt version 5.5.1 in /usr/local/lib
    
    

    When I switched the qmake command line to using the above and added below include

    #include <QtConcurrent>
    

    The project no longer compiles:

    newForm.cpp.cc:6:24: fatal error: QtConcurrent: No such file or directory
    

    Now that the post was created line by line it did not trigger the spam filter. Something with that spam filter is horribly wrong.


  • Moderators

    @nulluse said:

    The output of build process is showing qt4 in several places, is that because Netbeans is using qmake-qt4?

    Yeah, qmake-qt4 will link against Qt 4 libraries, which you don't want.

    newForm.cpp.cc:6:24: fatal error: QtConcurrent: No such file or directory
    

    That's a good sign; it shows that you have successfully switched to Qt 5.

    Qt Concurrent has been moved to a separate module in Qt 5 (like the Qt Widgets module). Add this to your .pro file: QT += concurrent

    Now that the post was created line by line it did not trigger the spam filter. Something with that spam filter is horribly wrong.

    Unfortunately, it looks like we have an overly-vigilant spam filter :( https://forum.qt.io/topic/65019/what-is-going-with-this-spam-detection-here


Log in to reply
 

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