跳到內容

General and Desktop

This is where all the desktop OS and general Qt questions belong.
83.4k 主題 456.4k 貼文
  • 此主題已被刪除!

    Solved
    2
    0 評價
    2 貼文
    4 瀏覽
  • qt C++: eventFilter does not see 'MouseMove' event

    Solved
    7
    0 評價
    7 貼文
    640 瀏覽
    E
    @Pl45m4 Problem Solved Re: qt C++: eventFilter does not see 'MouseMove' event Thanks to everyone who contributed to solving this issue. In summary, I created a subclass of QGraphicsView (named MyQGrphicsView) to capture mouse press and mouse move events, and needed to use QGraphicsScene (instead of QGraphicsView) and the viewport. I am providing the code below for a more detailed explanation. In this example, I used Qt Creator and created a QGraphicsView object in the main window, then promoted the object to MyQGraphicsView. When you run the example, a line is drawn in the window and when the mouse is pressed, released, or moved in the window the program outputs mouse information via qDebug. Here is the code. MyGraphicsView.h: #ifndef MYQGRAPHICSVIEW_H #define MYQGRAPHICSVIEW_H #include <QGraphicsView> #include <QObject> class MyQGraphicsView : public QGraphicsView { Q_OBJECT public: MyQGraphicsView(QWidget *parent = nullptr); ~MyQGraphicsView(); bool eventFilter(QObject *object, QEvent *event); public slots: private: QGraphicsScene *myScene; int moveCount; int pressCount; int releaseCount; }; #endif // MYQGRAPHICSVIEW_H MyGraphicsView.cpp: #include <QGraphicsSceneMouseEvent> #include "myqgraphicsview.h" MyQGraphicsView::MyQGraphicsView(QWidget *parent) : QGraphicsView(parent) { myScene = new QGraphicsScene(this); setScene(myScene); /* required in order to generate mouse events in myScene */ myScene->installEventFilter(this); /* required for eventFilter */ this->viewport()->setMouseTracking(true); /* required in order to generate GraphicsSceneMouseMove events */ moveCount = 0; /* for debugging */ pressCount = 0; /* for debugging */ releaseCount = 0; /* for debugging */ myScene->addLine(0, 100, 200, 200, Qt::SolidLine); /* (x1, y1, x2, y2, QPen) */ } MyQGraphicsView::~MyQGraphicsView() { delete myScene; /* required since QGraphicsScene does not take ownership of myScene (per QGraphicsScene::setScrene documentation) */ } bool MyQGraphicsView::eventFilter(QObject *object, QEvent *event) { if (object == myScene){ // press event if (event->type() == QEvent::GraphicsSceneMousePress) { const QGraphicsSceneMouseEvent* const me = static_cast<const QGraphicsSceneMouseEvent*>(event); const QPoint position = me->scenePos().toPoint(); pressCount++; qDebug("QEvent::GraphicsSceneMousePress %d, scene pos = %d, %d", pressCount, position.x(), position.y()); // your logic here } // release event else if (event->type() == QEvent::GraphicsSceneMouseRelease) { const QGraphicsSceneMouseEvent* const me = static_cast<const QGraphicsSceneMouseEvent*>(event); const QPoint position = me->scenePos().toPoint(); releaseCount++; qDebug("QEvent::GraphicsSceneMouseRelease %d, scene pos = %d, %d", releaseCount, position.x(), position.y()); // your logic here } // move event else if (event->type() == QEvent::GraphicsSceneMouseMove) { const QGraphicsSceneMouseEvent* const me = static_cast<const QGraphicsSceneMouseEvent*>(event); const QPoint position = me->scenePos().toPoint(); moveCount++; qDebug("QEvent::GraphicsSceneMouseMove count = %d, scene pos = %d, %d", moveCount, position.x(), position.y()); // your logic here } } return QGraphicsView::eventFilter(object, event); /* pass-on the event to the base class QGraphicsView */ } MainWindow.h: #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H MainWindow.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; } main.cpp: #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } MainWindow.ui: <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="MyQGraphicsView" name="graphicsView"> <property name="geometry"> <rect> <x>160</x> <y>90</y> <width>481</width> <height>281</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>26</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <customwidgets> <customwidget> <class>MyQGraphicsView</class> <extends>QGraphicsView</extends> <header location="global">MyQGraphicsView.h</header> </customwidget> </customwidgets> <resources/> <connections/> </ui>
  • 此主題已被刪除!

    Unsolved
    1
    0 評價
    1 貼文
    3 瀏覽
    尚無回覆
  • Is there any ways to change the outstyle of the window close button?

    Unsolved
    8
    0 評價
    8 貼文
    623 瀏覽
    nicker playerN
    @Pl45m4 I found that the dockwidet only could be added by the qmainwindow object.But in fact I used the qwidget to add the dockwidget.Are there any problems to use like this? And the way you just mentioned setFeatures dosent work.I dont know why. QWidget* t_TitleBar = this->titleBarWidget(); QWidget* t_DefaultWidget = new QWidget(this); setTitleBarWidget(t_DefaultWidget); setFeatures(QDockWidget::DockWidgetMovable |QDockWidget::DockWidgetFloatable |QDockWidget::DockWidgetClosable); delete t_TitleBar;
  • Public export of defines from QT6:Platform polutes my build

    Unsolved
    3
    0 評價
    3 貼文
    310 瀏覽
    C
    @Thestrup Given the message in the OpenNurbs header it seems straightforward enough, in your source code make sure that you #include <opennurbs.h> before you #include anything from Qt or the Windows SDK.
  • Switching DesktopSettingsAware colors for a button

    Unsolved
    2
    0 評價
    2 貼文
    183 瀏覽
    T
    @cadol001 try this: void MainWindow::changeEvent(QEvent *event) { if (event->type() == QEvent::PaletteChange) { QPalette palette = QApplication::style()->standardPalette(); palette.setColor(QPalette::Button, palette.color(QPalette::Base)); button->setPalette(palette); } event->ignore(); }
  • How to "break_up" single methoid / function...

    Unsolved
    8
    0 評價
    8 貼文
    470 瀏覽
    mzimmersM
    @AnneRanch said in How to "break_up" single methoid / function...: whatever I pick I still have to "split" the current function I have now, correct ? Seems to me that it depends on what you mean by "split" - if you use the above suggestions, you can retain a single worker function that does all the HW stuff you mention. As that function makes progress, it can send signals to the main thread to update the UI. So, if I understand the use case, all the "work" can be performed within a single function.
  • 此主題已被刪除!

    Unsolved
    1
    0 評價
    1 貼文
    2 瀏覽
    尚無回覆
  • Save Programm Settings internally + how to organize with folders?

    Solved qt c++ folder settings
    18
    0 評價
    18 貼文
    2k 瀏覽
    S
    @StudentScripter said in Save Programm Settings internally + how to organize with folders?: QString filePath = "/path/to/your/settings.ini"; If you want to use an ini file, please use QStandardPath with ConfigLocation and don't invent your own place where to put the ini file.
  • troubles with QFile::setPermissions

    Unsolved
    10
    0 評價
    10 貼文
    789 瀏覽
    C
    @rock37 If you are only interested if the ReadUser flag, regardless of platform, is set then only test for that. Something like: QVERIFY(flags & QFileDevice::ReadUser);
  • How to download examples

    Unsolved
    10
    0 評價
    10 貼文
    9k 瀏覽
    S
    anyone still having trouble downloading examples, can find it here: https://code.qt.io/cgit/qt/qtbase.git/ bottom left you will find. git://code.qt.io/qt/qtbase.git http://code.qt.io/qt/qtbase.git https://code.qt.io/qt/qtbase.git use git client to download.
  • Using qt to make an interface to modify ns3 parameters

    Unsolved
    6
    0 評價
    6 貼文
    450 瀏覽
    Pl45m4P
    @jiliguli Have you read about NS3 and Qt integration already? https://www.nsnam.org/docs/tutorial/singlehtml/#overview https://forum.qt.io/topic/30865/integrating-ns3-with-qt https://www.nsnam.org/wiki/HOWTO_configure_QtCreator_with_ns-3 You could link your Qt and the NS3 project and build your own GUI that uses NS3 (I have no experience regarding this, but I think it might work)
  • How to choose which columns to display and in which order when using QSqlRelationalTableModel?

    Solved sql
    8
    0 評價
    8 貼文
    731 瀏覽
    JonBJ
    @jdent but I don't'see a method moveColumn() bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn, const QModelIndex &destinationParent, int destinationChild) But (and I see @SGaist has just posted) you don't want to move columns around in the model to change the order they are displayed. You do this in the view, the QHeaderView which is the QTableView::horizontalHeader() , by moving its sections.
  • Need to keepalive a QWebSocket?

    Solved
    2
    0 評價
    2 貼文
    199 瀏覽
    SGaistS
    Hi, I think it's the ping method you are looking for.
  • Audio does not work if run from cron or systemd

    Unsolved
    9
    0 評價
    9 貼文
    573 瀏覽
    V
    This is the only solution that worked for me. I use systemd to call a script that starts the QT program, and my WebEngineView didn't output audio at all. After using this solution (exporting all my env variables inside the script before calling the QT program) the problem was solved. I wanted to go a little further, and after a lot of trial and error, I discovered that the only variable I needed to set was HOME. So, I added this to my script and everything worked. export HOME=/home/root Thanks @Mark81
  • What is the best way to display a Join between 2 or 3 tables? QSqlQueryModel?

    Solved sql
    4
    0 評價
    4 貼文
    547 瀏覽
    JonBJ
    @jdent said in What is the best way to display a Join between 2 or 3 tables? QSqlQueryModel?: Ok, I thought there was more explicit support for joins Yes, there isn't :) Qt provides low-ish level support for database table access, nothing fancy, nothing for an arbitrary JOIN. The only "extra" is QSqlRelationalTableModel Class, which implements a specific JOIN for a foreign key. There will be third-party code, e.g. KDAB, out there which builds another layer on top if you want it.
  • Why does valgrind crash

    Unsolved
    7
    0 評價
    7 貼文
    840 瀏覽
    ocgltdO
    I found a clue here: Debugging Code that Uses QRegularExpression QRegularExpression internally uses a just in time compiler (JIT) to optimize the execution of the matching algorithm. The JIT makes extensive usage of self-modifying code, which can lead debugging tools such as Valgrind to crash. You must enable all checks for self-modifying code if you want to debug programs using QRegularExpression (for instance, Valgrind's --smc-check command line option). The downside of enabling such checks is that your program will run considerably slower. To avoid that, the JIT is disabled by default if you compile Qt in debug mode. It is possible to override the default and enable or disable the JIT usage (both in debug or release mode) by setting the QT_ENABLE_REGEXP_JIT environment variable to a non-zero or zero value respectively. I'll refine my question and post something new! and more focused!
  • Compilation error with Qt5.15.12 on MacOS - problems with qglobal.h

    Unsolved qt5 c++qt errors
    1
    0 評價
    1 貼文
    555 瀏覽
    尚無回覆
  • How to distinguish Left-Shift and Right-Shift in QShortcut ?

    Unsolved
    2
    1 評價
    2 貼文
    170 瀏覽
    Pl45m4P
    @sonichy said in How to distinguish Left-Shift and Right-Shift in QShortcut ?: connect(new QShortcut(QKeySequence(Qt::SHIFT_Left + Qt::Key_Down), this), SIGNAL(activated()), this, SLOT(moveBottomDown())); Unfortunately there is no difference in QKey between left and right shift. There is only Qt::ShiftModifier. In this topic here they suggest to use the OS native key codes / events. https://forum.qt.io/topic/36639/read-keyboard-input-that-can-differ-between-left-shift-and-right-shift-key
  • Stylesheet problem with transparent background

    Unsolved
    17
    0 評價
    17 貼文
    3k 瀏覽
    G
    @Tink The documentMode seems to solve quite some issues. So, thanks a lot. However, i believe there is still a bug somewhere related to the transparent backgrounds. Thanks.