0

I am trying to simulate save functionality(Tool-Save) using Qtest but not able to find any pointer

#!/usr/bin/python
from PySide2.QtCore import Qt, QEvent
from PySide2.QtWidgets import QApplication
from PySide2.QtGui import (QTextCharFormat, QIcon, QKeySequence,
                           QBrush, QColor, QTextDocument, QFont,
                           QTextCursor, QTextBlockFormat, QFontDatabase)
from PySide2.QtWidgets import (QPlainTextEdit, QSizePolicy, QApplication, QLabel, QGridLayout, QMessageBox, QToolBar, QTextEdit, QCheckBox, QAction,
                               QTableWidget, QTableWidgetItem, QHeaderView, QMenu,
                               QWidget)
from PySide2.QtCore import Qt, QSize, QRegExp, QFile, QTextStream, QRect
from PySide2.QtWidgets import (QMainWindow, QVBoxLayout,
                               QPlainTextEdit, QGridLayout, QGroupBox,
                               QListWidget, QHBoxLayout, QLabel, QLineEdit,
                               QMenuBar, QPushButton, QMessageBox, QDialog,
                               QTextEdit, QVBoxLayout, QWidget, QFormLayout,
                               QCheckBox, QDialogButtonBox,
                               QTableWidget, QTableWidgetItem, QHeaderView)
import sys


class Window(QMainWindow):
    def __init__(self, app):
        super().__init__()


    def create_widget(self):
        self.create_menu()
        self.plain_textedit = QPlainTextEdit()
        self.plain_textedit.setMouseTracking(True)
        self.plain_textedit.viewport().installEventFilter(self)
        main_layout = QVBoxLayout()
        main_layout.addWidget(self.toolbar)
        main_layout.addWidget(self.plain_textedit)
        self.window = QWidget()
        self.window.setLayout(main_layout)
        self.setCentralWidget(self.window)


    def create_menu(self):
        """
        create menu bar into window
        """
        self.toolbar = QToolBar()
        self.toolbar.setStyleSheet("QToolBar {background: #ff8000}")
        self.addToolBar(Qt.TopToolBarArea, self.toolbar)
        menu = QMenu("&Tool", self)
        self.menuBar().addMenu(menu)
        self.actionSave = QAction(QIcon.fromTheme('document-save'),
                                  "&Save", self, shortcut=QKeySequence(Qt.CTRL+Qt.Key_W),
                                  triggered=self.save_data, enabled=True)
        self.toolbar.addAction(self.actionSave)
        menu.addAction(self.actionSave)
        menu.addSeparator()
    
    def save_data(self):
        pass
        
        
    def eventFilter(self, obj, event):
        if obj is self.plain_textedit.viewport():
            print ("plain_1")
       

def main():
    app = QApplication([])
    window = Window(app)
    window.create_widget()
    window.show()
    app.exec_()


if __name__ == "__main__":
    sys.exit(main())

enter image description here

#!/usr/bin/python 
import os
env = os.environ
import unittest
import logging
from PySide2.QtWidgets import QApplication
from PySide2.QtTest import QTest
from PySide2.QtCore import Qt, QEvent
from debug2 import Window

class test(unittest.TestCase):
    def setUp(self):
        self.app = QApplication.instance()
        if self.app is None:
            self.app = QApplication([])

        self.window = Window(self.app)
        self.window.create_widget()
        self.window.show()
        #to open gui and helpful for debug
        #self.app.exec_()

    def tearDown(self):
        self.app.deleteLater()

    def test_save_data(self):
        actions = self.window.menuBar().actions()
        for action in actions:
            sub_commands = action.menu().actions()
            for sub_command in sub_commands:
                if sub_command.text() == "&Save":
                    print (dir(QTest))
                    QTest.mouseClick(self.window.window, Qt.LeftButton)
                #print (sub_command, sub_command.objectName(), sub_command.text())

if __name__ == '__main__':
    unittest.main()
user765443
  • 1,856
  • 7
  • 31
  • 56
  • 1
    [`mouseClick()`](https://doc.qt.io/qt-5/qtest.html#mouseClick) just clicks on a widget, and defaults to posision `(0, 0)`: you're just clicking on the top left corner of the window. You need to click on the action for the menu you want to open in the menubar, and then the action. The rectangle of the menubar action is retrieved with [`actionGeometry()`](https://doc.qt.io/qt-5/qmenubar.html#actionGeometry), similarly to the action of the menu, [`actionGeometry()`](https://doc.qt.io/qt-5/qmenu.html#actionGeometry). – musicamante Dec 06 '21 at 20:42
  • I have tried but it is not working can u please add some more info – user765443 Dec 07 '21 at 15:52
  • actions = self.window.menuBar().actions() for action in actions: sub_commands = action.menu().actions() for sub_command in sub_commands: if sub_command.text() == "&Save": action_cmd = action.menu().actionGeometry(sub_command) QTest.mouseClick(self.window.toolbar, Qt.LeftButton, Qt.NoModifier, action_cmd.center()) – user765443 Dec 07 '21 at 16:10

0 Answers0