0

I have a QMainWindow with a toolbar, and I'm having troubles with fitting the QPixmap to the window, such that it won't tackle with the toolbar.

I want to display the picture:

enter image description here

And from the code:

import sys
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import QAction, QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QPainter


class Menu(QMainWindow):

    def __init__(self):
        super().__init__()
        newAct = QAction('New', self)
        self.toolbar = self.addToolBar('Remove')
        self.toolbar.addAction(newAct)
        self.image = QPixmap("background.png")
        self.setGeometry(100, 30, 500, 300)
        self.resize(self.image.width(), self.image.height())
        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        rect = QRect(0, 0, self.image.width(), self.image.height())
        painter.drawPixmap(rect, self.image)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = Menu()
    sys.exit(app.exec_())

I get:

enter image description here

And as you can see, The picture is on the toolbar as well, and I don't want that.

Another try:

import sys
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import QAction, QMainWindow, QApplication
from PyQt5.QtGui import QPixmap, QPainter


class Menu(QMainWindow):

    def __init__(self):
        super().__init__()
        newAct = QAction('New', self)
        self.toolbar = self.addToolBar('Remove')
        self.toolbar.addAction(newAct)
        self.image = QPixmap("background.png")
        self.setGeometry(100, 30, 500, 300)
        self.resize(self.image.width(), self.image.height() + self.toolbar.height())
        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        rect = QRect(0, self.toolbar.height(), self.image.width(), self.image.height() + self.toolbar.height())
        painter.drawPixmap(rect, self.image)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = Menu()
    sys.exit(app.exec_())

But I get:

enter image description here

And as you can see, I don't see one of the lines (the blue one).

How can I fix it, so the picture will fit the window excluding the toolbar? In addition to that, it means I'll have to change all my mouse clicks to move the y-axis. Is there perhaps a way I can set everything such that (x,y)=(0,0) would be at the uppermost left, below the toolbar?

I'm using Python 3.6.5 |Anaconda custom (64-bit) on windows | PyQt version: 5.9.2

sheldonzy
  • 5,505
  • 9
  • 48
  • 86

1 Answers1

1

Although I can not reproduce the problem, the following solution must work, in it I draw the image in a widget and I set them as centralwidget.

import sys
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import QAction, QMainWindow, QApplication, QWidget
from PyQt5.QtGui import QPixmap, QPainter

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.image = QPixmap("background.png")
        self.setFixedSize(self.image.size())

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.image)


class Menu(QMainWindow):
    def __init__(self):
        super().__init__()
        newAct = QAction('New', self)
        self.toolbar = self.addToolBar('Remove')
        self.toolbar.addAction(newAct)
        self.setCentralWidget(Widget())
        self.setFixedSize(self.sizeHint())
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = Menu()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241