0

I have a ui I am working on, and I am trying to get the available buttons to change when I press one of the current buttons. For example, if buttons 1-9 are shown, if I press button 2, I want buttons 10-13 to show and buttons 1-9 to disappear. I am using qtdesigner, but I can hardcode this if needbe, I just dont know how to get this without opening and closing the entire window

Here are my buttons:

Buttons

Thanks!

ChrisM
  • 125
  • 14
  • Can you show your code please – eyllanesc Jul 11 '17 at 02:34
  • If you press another button you want to do? – eyllanesc Jul 11 '17 at 02:35
  • I dont have code that I have actually written, I have just been using QT designer. If you would like, I can covert the .ui to .py and show you that. When I click a button, I want another set of buttons to appear in their place. So if I have those 9 shown, and I press a button, I want a different set of say 4 buttons to display and the original 9 to disappear – ChrisM Jul 11 '17 at 03:39
  • What I ask is that you show what you have tried to solve your problem, since you are the main interested. – eyllanesc Jul 11 '17 at 04:51
  • I dont have code for that because I have only been working in designer up till now, so my attempts in designer cant really be shown. But I can say if I had tried code, which may very well be the next step, I wouldnt have much of an idea of where to go because python and pyQt are relatively new to me – ChrisM Jul 11 '17 at 15:46
  • So before asking for a solution I recommend you study more python and pyqt before asking questions here. To ask good questions read the following: https://stackoverflow.com/help/how-to-ask – eyllanesc Jul 11 '17 at 17:26
  • Well Im asking here to learn. This is a project of mine that I am undertaking to learn python and pyqt. I figured the best way to understand how these work is to actually work with them and get help from people who already have experience in it, the videos and guides I have read on the subject havent shown me how to do this specifically – ChrisM Jul 11 '17 at 18:40
  • In SO we hope that the author of the question tries to solve his problem first, since he is the main interested, later we help with comments, proposals of solutions etc. – eyllanesc Jul 11 '17 at 19:27
  • I have tried to figure it out, and I am just stuck. The only thing I have figured out how to do is close the window and open a new window, as mentioned. But at the same time, I am still trying to figure it out. If you have any hints at the very least I would appreciate it – ChrisM Jul 12 '17 at 03:13
  • Read about signals in PyQt so that you can handle the clicked signal of a QPushButton, also looks for information on how to clean a QLayout and how they are added. – eyllanesc Jul 12 '17 at 03:16
  • So have a signal for each button that clears the current layout and opens a new one? Do I need to save the layout somehow for this or is this something I do hardcoded? – ChrisM Jul 12 '17 at 03:49
  • You can get the layout from the widget with the `layout()` function, it would be good to ask me by showing me your progress to understand you better. – eyllanesc Jul 12 '17 at 13:08
  • I decided to just try hardcoding this bit, and discovered (with the help of the answers on here) that hide() and show() seem to be the properties and functions I was looking for. I will keep posting on here as I explore further. Thank you for the help so far – ChrisM Jul 14 '17 at 17:01

1 Answers1

1

The easiest way to do that is,

  1. Have the layout you need set up.
  2. Add all the (N) buttons to the layout itself from the very beginning.
  3. Initially hide the buttons you don't want to show up.
  4. When clicking in the button you want, hide the buttons you want to disappear and show the buttons you want to appear.

Obs: Remember that when hiding the buttons all of the other will adjust itself according to the layout's policies.

Another way to do that would be really adding and removing the buttons you need:

  1. Add the button you need to your layout.

layout.addWidget(btn)

  1. When you want to remove the button

layout.removeWidget(btn)

btn.hide()

btn.close()

del btn

Obs: you will have to struggle a bit more adding and removing the buttons from your layout but that's how it is.

There is also the option of instead of putting all the buttons in layouts you divide it in groups and put it in another widget(making it as layers) so when you have to remove certain amount of buttons you just remove that widget and all from that will be gone with it. Also you can have a look in ButtonGroup and see if it'd serve you for something.

yurisnm
  • 1,630
  • 13
  • 29
  • I appreciate the feedback, unfortunately I plan to do this for multiple buttons with multiple more appearing... There might well be over 50 buttons and adding them all at once to a layout seems counter productive... With a small amount, this would be an easy fix! – ChrisM Jul 11 '17 at 15:48
  • It's not kind of a work around it's really how it's advised, but as you said you have a lot of buttons, what you can do is really add and remove buttons whenever you need, I'll be editing the answer with some other tips and see if it makes sense. – yurisnm Jul 12 '17 at 12:45
  • I've struggled with it for a while, add and remove widgets from layout is a pain in... I'd recommend you to put the easiness of hide and showing group of buttons and memory saving in a balance and see how it would affect what you need ") – yurisnm Jul 12 '17 at 13:00
  • Ahhhh I didnt realize that hiding a button actually acted like it was removed from the layout, in the sense that the other buttons get moved around as if the hidden one wasnt there. Thats exactly what I needed, so what Im thinking is having a function for each set of buttons that shows/hides the set. So if I press one button, it will bring up the second set by hiding the first set using hide() and showing the second set using show(). Is there a reason to actually remove the button from the layout for what I am trying to achieve? As in what difference will I see in the ui? Thank you! – ChrisM Jul 14 '17 at 17:00
  • Your reasoning is perfect, that's exactly what I tried to pass to you but maybe I didn't express myself very well haha. About removing it and putting it back I don't recommend that, it'll just consume memory and processing because you will be anyway creating all the buttons deleting and having to create it again, and so on... The way you described how you are going to do is perfect. If need any help just let me know ") – yurisnm Jul 14 '17 at 19:19
  • Thanks again. Do you know the best way to split up the project into multiple files? I was originally thinking of putting the buttons in one file, mainwindow stuff in another, and other widgets of the same kind in their own... But im running into problems... – ChrisM Jul 15 '17 at 01:22
  • Of course, there is no headache to do that, you just gotta put each object you want in the respective files you want and import each other where you need. I'd recommend you to have a central "guy" that know everyone, let's say you have a QMainWindow object that contains all your all your objects from where you import each one of them. It's kinda hard to explain like this out of blue without knowing your needs and all that. But that's it, just import them. Take care with cicle loop, you cant do "A --import-> B" and also "B ---import--->A" , it will break everything. – yurisnm Jul 16 '17 at 05:03