#๐Ÿ”’ Pyside6 question

147 messages ยท Page 1 of 1 (latest)

tepid pier
#

My app creates a bunch of QWidgets names the same thing and I want each one to have its own X, Y, and Z "attributes" I think I'm supposed to have each widget be an instance of a class that has class attributes. Hope that made sense.

scenic mantleBOT
#

@tepid pier

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

frozen mason
tepid pier
#

You recommended learning about OOP and I did, I'm not sure if I'm applying the right concepts or the concepts correctly to my app.

#

my name used to be Aizi

frozen mason
#

ahh ok yeah I remember you ๐Ÿ™‚

#

It's still a bit unclear what you're asking though?

humble isle
#

are u sure its not just X and Y coordinates and not a Z?

frozen mason
#

@tepid pier still there?

tepid pier
tepid pier
tepid pier
frozen mason
#

That's usually done through the sizeHint of the widget

#

do you have a custom class for your widget?

#

@tepid pier ?

tepid pier
#

Okay, Il keep that in mind next time, I don't, but I had a feeling as I mentioned above I need a class for it, when you say custom, do you just mean the widget having its one class?

frozen mason
#

Perhaps you might still be misunderstanding how classes are used

#

Do you understand what an instance is?

#

@tepid pier ?

#

These help threads are meant to be active conversations. It's very difficult to help if you disappear after each message

tepid pier
#

Sorry, I'm multitasking. I do, it's a variable that that accesse a class? Right? I hope thats right ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚

humble isle
#

the variable is called a handler

frozen mason
#

A Class is a blueprint/template, and an instance is the actual construction from that blueprint

frozen mason
#

do you know what str is?

tepid pier
#

Yes it's a data type, it's used for things like words

frozen mason
#

!e

print(str)
scenic mantleBOT
frozen mason
#

yes, it's a class

#

classes are definitions of datatypes

tepid pier
#

Yeah

frozen mason
#

so str is a class

tepid pier
#

Oh si

frozen mason
#

but something like "abc" is an instance of that class

#

123 is an instance of int class

#

QPushButton is a class

#

QPushButton() is an instance of that class

tepid pier
#

As far as using it in the purpose I'm wanting

frozen mason
#

ok, so when dealing with classes in a pyside gui, we typically inherit our classes from existing pyside classes

frozen mason
#

so my main window can inherit from QDialog (or QMainWindow)

tepid pier
#

Yup

frozen mason
#
from PySide6 import QtWidgets, QtCore, QtGui
from PySide6.QtCore import Qt

class Window(QtWidgets.QDialog):

    def __init__(self):
        super().__init__()


app = QtWidgets.QApplication()
win = Window()
win.show()
app.exec()
#

so here's my very basic Window class

#

I create an instance like win = Window()

#

and then I show it

#

If I want a bunch of widgets with similar functionality but might have slightly different data, I can create a new class for that too

#
class InfoCard(QtWidgets.QFrame):

    def __init__(self, title):
        super().__init__()

        self.title = title
        self.setFrameStyle(QtWidgets.QFrame.Box)

        layout = QtWidgets.QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)

        title_label = QtWidgets.QLabel(title)
        title_label.setFont(QtGui.QFont('segoe ui', 14))
        layout.addWidget(title_label)
#

here's a very simple class example of that

#

it's a simple widget that displays a title, and draws a box around the widget

#

I can create many instances of these

tepid pier
#

If I have two instances of the same class and have them access a variable. If instance one sets it to "1" and the second one sets it to "2" is the variable 2 or is the variable individual to the instance of the class. (That's what I'm hoping)

tepid pier
frozen mason
#

What exactly are you trying to achieve?

frozen mason
#
class Window(QtWidgets.QDialog):

    def __init__(self):
        super().__init__()

        layout = QtWidgets.QVBoxLayout(self)

        for i in range(1, 5):
            card = InfoCard(f"Title {i}")
            layout.addWidget(card)
#

look at this update to my Window class

#

I create 4 instances of InfoCard

#

they each have a unique title

frozen mason
#

so it creates 4 identical widgets, just with the text that's different

#

What exactly do you want to happen? What change needs to affect what?

tepid pier
# frozen mason What exactly are you trying to achieve?

Each time the user clicks on the calendar to add another identical widget on the calendar, each one of these carbon copy QWidgets needs to have its own variables that track numbers used by the code for expanding the widget. I need to be able to have variables all named the same thing, because the widgets are carbon copies, but are individually associated with their widget so. Each widget gets their own set of variables.

#

Did that make sense?

frozen mason
#

Why are you putting so much extra effort into the size? It should all auto-expand for you

#

you shouldn't have to manually resize anything just because you add a new widget

tepid pier
#

Yk like on a basic calendar app

frozen mason
#

ahh, so the widget represents a position/event on the calendar

tepid pier
#

So when I made the original code it worked fine with one event, with it's set of variables, until I put multiple events on the calendar. Which messed with the set of variables. So I'm like, then each one needs to have their own set of variables when the code carbon copies one onto the calendar.

frozen mason
#

That starts to get tricky when you're dealing with overlapping widgets and positioning

frozen mason
#

I would either be using custom paint events to draw the calendar events, or perhaps even a QGraphicsScene

#

it adds a ton of complexity, but you get way more control

tepid pier
#

Well each event needs to be able to have Qinputfield and stuff so that they can fill it out with details. Back to the variable issue?

frozen mason
#

No, the main issue is how to display the widget

#

storing the information in the instance is trivial

#

You can create custom signals and have your main calendar listen for changes

tepid pier
frozen mason
#

Yeah, but QWidget is great when you need to put a bunch of widgets in a layout

#

it's not ideal for trying to draw a bunch of overlapping widgets on a calendar

tepid pier
frozen mason
#

What if two events are happening at the same time?

tepid pier
frozen mason
tepid pier
tepid pier
# frozen mason

Honestly that doesn't look to hard, as long as you can set which widget goes on top

frozen mason
#

This solution is where I'd suggest QGraphicsScene, but it's a completely different workflow than standard QWidgets

tepid pier
frozen mason
#

yes

#

It's far more advanced though

tepid pier
# frozen mason yes

When I've already worked so hard on this, what would be the incentive in scrapping it all and starting over with QGraphicsScene? I've spent weeks learning how to do make this

frozen mason
#

When you spend a month on a single gui, it's likely you've already forgot everything you did at the start

tepid pier
frozen mason
#

I'm excited about redoing things or moving onto new things so I can implement all the new things I've learned

frozen mason
#

which probably wasn't a great idea

tepid pier
#

Yeah probably

frozen mason
#

Try a few simple GUIs as practice

#

then come back to this

tepid pier
#

But why is QGraphicsScene superior? It's working so far

#

Like I'd be throwing away working code

#

For a different type of working code

#

No affence

#

Offence*

frozen mason
#

if you want to implement exactly what you have in mind, it's not really possible with the way you've currently been going

frozen mason
tepid pier
#

I'm pretty disheartened, how do I avoid spending weeks on a project, then find out Im doing it wrong? When there are so many different ways to do/achieve/create something.

frozen mason
#

It's unrealistic to expect you're going to create your first project perfectly

#

use every project as a learning opportunity

#

I've made 100s of GUI

tepid pier
#

Yeah I guess

frozen mason
#

many are not good

tepid pier
#

Okay

frozen mason
#

but now I can create GUI very quickly, generally with the right mindset

#

you didn't even know what classes were when you started this project

#

which is pretty much a requirement for pyside gui

tepid pier
#

So I have the lines on the calendar made with Qpaint, will I need to scrap that too to use the QGraphicsScene or can they work together.

frozen mason
#

If you're already used to working with qpaint, you can try and draw all your calendar events using that way too

#

otherwise you'd have to scrap it (although the logic would be similar for qgraphics)

tepid pier
#

It's something I'm working on

frozen mason
#

QGraphicsScene is like a window into a drawing canvas

frozen mason
tepid pier
frozen mason
#

you wouldn't have a nice way to interact with already created widgets

#

but qgraphicsitem is immediately interactive and you can emit signals when they are clicked

tepid pier
#

Okay, ๐Ÿ˜ญ๐Ÿ˜ญ๐Ÿ˜ญ๐Ÿ˜ญ

#

Well thanks, I guess

frozen mason
#

here's a nice intro to qgraphics

tepid pier
#

Sweet

frozen mason
#

It's going to take time to learn. PySide is MASSIVE

#

I've been using it for 5+ years now and I still learn new things all the time

tepid pier
#

Yeah

#

Welp it's 1 am, bye and thanks again

scenic mantleBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.