如何做出炒股软件附图指标的那种小窗口

子窗口显示的只有这么一点如何做出炒股软件附图指标的那种小窗口,可以跟随主窗口移动并且可以根据主窗口的大小而变化。
我是通过将一个QWidget的子窗口嵌入QMainWindow主窗口的思路,但是我发现:
update_size方法中设置了子窗口的位置和大小,但是却没有按照我设置的尺寸显示,而且self.setWindowFlags(Qt.FramelessWindowHint)
这段代码也无效,问过ai了,给的三个方法,就这个还能显示,其他的连子窗口都不显示,之前在pyqt5中这段代码就可以运行,现在转到pyside6中
就不行了,实在找不到原因了

import os
import sys
import pandas as pd
import locale
de = locale.getpreferredencoding()

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *

class MainWindowA(QMainWindow):
def init(self):
super().init()
self.init_ui()

def init_ui(self):
    self.setWindowTitle("主窗口 B")
    screen = QApplication.primaryScreen().geometry()
    self.screenWidth = screen.width()
    self.screenHeight = screen.height()
    self.setGeometry(0, 0, int(self.screenWidth * 0.8), self.screenHeight)

    self.mdi_area = QMdiArea()
    self.setCentralWidget(self.mdi_area)
    #

self.MainWindowB = MainWindowB()
self.mdi_area.addSubWindow(self.MainWindowB)

    # self.MainWindowB.show()

def resizeEvent(self, event):
super().resizeEvent(event) # 调用父类的 resizeEvent 方法
new_size = event.size()
new_width = new_size.width()
new_height = new_size.height()

    self.MainWindowB.update_size(new_width, new_height)   # 自由指标窗口

class MainWindowB(QWidget):
def init(self):
super().init()
self.setWindowFlags(Qt.FramelessWindowHint)

    # self.init_ui()

def init_ui(self):
self.setWindowTitle(“副窗口 B”)

def create_painter(self, qp, color, width):
    qp.save()
    pen = QPen(QColor(color), width)
    qp.setPen(pen)
    return qp

def paintEvent(self, event):
    qp = QPainter(self)
    # qp.setRenderHint(QPainter.Antialiasing)

self.create_painter(qp, Qt.red, 2).drawLine(0, self.height() -100, self.width()-600, self.height() -100)
print(‘a’)

def update_size(self, new_width, new_height):
    y1 = int(new_height * 0.975)
    y2 = int(new_height * 0.10417)
    self.setGeometry(0, y1, new_width, y2)
    self.update()
    # MainWindowB子窗口为什么没有按照我设置的位置和大小显示

if name == ‘main‘:
app = QApplication(sys.argv)
main_window_a = MainWindowA()
main_window_a.show()
sys.exit(app.exec())

Jason990420
最佳答案

Set All widgets mouse tracking to True !

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QApplication, QMainWindow, QMdiArea,
    QMdiSubWindow, QWidget, QVBoxLayout, QLabel)


class MainWindowA(QMainWindow):

    windows = []

    def __init__(self, title="主窗口"):
        super().__init__()
        screen = QApplication.primaryScreen().geometry()
        w, h = screen.width(), screen.height()
        self.setGeometry(w//4, h//4, w//2, h//2)
        self.setWindowTitle(title)
        self.add_windows()

    def add_windows(self):
        self.mdi_area = QMdiArea()
        self.setCentralWidget(self.mdi_area)
        for row in (("A", "B"), ("C", "D")):
            line = []
            for col in row:
                child_window = Child_Window(f"副窗口 {col}")
                line.append(child_window)
                self.mdi_area.addSubWindow(child_window)
            self.windows.append(line)

    def resizeEvent(self, event):
        size = event.size()
        width, height = size.width(), size.height()
        rows = len(self.windows)
        for i, row in enumerate(self.windows):
            cols = len(row)
            for j, win in enumerate(row):
                win.setGeometry(
                    j*width//(cols+1), i*height//rows, width//(cols+1), height//rows)

class Child_Window(QMdiSubWindow):

    def __init__(self, title):
        super().__init__()
        self.title = title
        self.setWindowTitle(title)
        widget = QWidget()
        layout = QVBoxLayout(widget)
        self.label = QLabel()
        self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        layout.addWidget(self.label)
        widget.setLayout(layout)
        self.setWidget(widget)
        # Set All widgets mouse tracking to True
        self.label.setMouseTracking(True)
        widget.setMouseTracking(True)
        self.setMouseTracking(True)

    def mouseMoveEvent(self, event):
        self.x, self.y = event.x(), event.y()
        self.label.setText(f"({self.x}, {self.y})")

app = QApplication([])
main_window_a = MainWindowA()
main_window_a.show()
app.exec()

file

1个月前 评论
讨论数量: 4
Jason990420

Example Code

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QMdiArea, QMdiSubWindow

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("主窗口 B")
        screen = QApplication.primaryScreen().geometry()
        self.width, self.height = screen.width()//2, screen.height()//2
        x0, y0 = self.width//2, self.height//2
        self.setGeometry(x0, y0, self.width, self.height)

        self.mdi_area = QMdiArea()              # Main container for child windows
        self.child_window = QMdiSubWindow()     # Child window
        self.mdi_area.addSubWindow(self.child_window)
        self.setCentralWidget(self.mdi_area)
        self.child_window.setWindowTitle("副窗口 B")

    def resizeEvent(self, event):
        size = event.size()
        new_width = size.width()//2
        new_height = size.height()//2
        x0, y0 = new_width//2, new_height//2
        self.child_window.setGeometry(x0, y0, new_width, new_height)

app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())

file

1个月前 评论
rxts (楼主) 1个月前
rxts (楼主) 1个月前
Jason990420

Set All widgets mouse tracking to True !

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (QApplication, QMainWindow, QMdiArea,
    QMdiSubWindow, QWidget, QVBoxLayout, QLabel)


class MainWindowA(QMainWindow):

    windows = []

    def __init__(self, title="主窗口"):
        super().__init__()
        screen = QApplication.primaryScreen().geometry()
        w, h = screen.width(), screen.height()
        self.setGeometry(w//4, h//4, w//2, h//2)
        self.setWindowTitle(title)
        self.add_windows()

    def add_windows(self):
        self.mdi_area = QMdiArea()
        self.setCentralWidget(self.mdi_area)
        for row in (("A", "B"), ("C", "D")):
            line = []
            for col in row:
                child_window = Child_Window(f"副窗口 {col}")
                line.append(child_window)
                self.mdi_area.addSubWindow(child_window)
            self.windows.append(line)

    def resizeEvent(self, event):
        size = event.size()
        width, height = size.width(), size.height()
        rows = len(self.windows)
        for i, row in enumerate(self.windows):
            cols = len(row)
            for j, win in enumerate(row):
                win.setGeometry(
                    j*width//(cols+1), i*height//rows, width//(cols+1), height//rows)

class Child_Window(QMdiSubWindow):

    def __init__(self, title):
        super().__init__()
        self.title = title
        self.setWindowTitle(title)
        widget = QWidget()
        layout = QVBoxLayout(widget)
        self.label = QLabel()
        self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        layout.addWidget(self.label)
        widget.setLayout(layout)
        self.setWidget(widget)
        # Set All widgets mouse tracking to True
        self.label.setMouseTracking(True)
        widget.setMouseTracking(True)
        self.setMouseTracking(True)

    def mouseMoveEvent(self, event):
        self.x, self.y = event.x(), event.y()
        self.label.setText(f"({self.x}, {self.y})")

app = QApplication([])
main_window_a = MainWindowA()
main_window_a.show()
app.exec()

file

1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!