How to Add & Remove Qt Widgets Dynamically at Runtime

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ธ.ค. 2024

ความคิดเห็น • 93

  • @Postermaestro
    @Postermaestro 2 ปีที่แล้ว +3

    Wow this was exceptionally clear and concise, and you touch quickly on everything that might otherwise be confusing or unclear.

  • @andrewkelly2022
    @andrewkelly2022 ปีที่แล้ว

    Watched this just at the right time for me to learn a thing or two. Thank you!

  • @전승권-s6i
    @전승권-s6i 6 หลายเดือนก่อน

    what a beautiful work! But I keep get warning
    QLayout: Attempting to add QLayout "" to QFrame "frame", which already has a layout
    After I added QHBoxLayout* layout = new QHBoxLayout(ui->frame); in my code.
    Any help ??

  • @MasterHD
    @MasterHD 2 ปีที่แล้ว +1

    I'm curious as to how calling "delete button;" @7:11 works. It's just freeing the memory at the pointer, not even setting the pointer to null and definitely not informing the parent frame that it has been deleted. How is that working properly here? Is it better to use removeWidget()?

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว +1

      I understand what your concern is. I believe when the object is being deleted, that handles the "inform the parent" part, which does the rest of the work to remove it and so on.
      I don't remember exactly, but you could remove widget and then delete the pointer afterwards. I do think that simply deleting it handles the rest just because they have parent/child references to each other.

  • @rahultandon9749
    @rahultandon9749 ปีที่แล้ว

    Among the best videos on QT, Tnx

  • @ruzindanajules7040
    @ruzindanajules7040 4 ปีที่แล้ว +1

    i love your qt series man ,keep doing them if you can👍

    • @VelcodeCS
      @VelcodeCS  4 ปีที่แล้ว +1

      Glad to be of help! I do plan to keep posting the things I've learned while using this framework.

  • @saelethx3128
    @saelethx3128 2 ปีที่แล้ว

    Oh my god thank you, this video is so well done and clearly explained. All props to you man :) !!

  • @kingnebuchadnezzar6098
    @kingnebuchadnezzar6098 2 ปีที่แล้ว +1

    thanks for the video man, i want to make the user select the checkboxes and one additional button click to delete the layout and the widgets on the selected checkboxs hashmap. how can i do such thing ? connecting signal to signal and then signal to slot maybe ? can you give me an idea ?

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      You can follow the same approach on the video. You should be able to know which check boxes are checked (by checking if they're checked), and, if they are, you delete them and the layout/widget they're in.

  • @MinhNguyen-qm5hd
    @MinhNguyen-qm5hd 3 ปีที่แล้ว +1

    Thank you man, I was stuck in my project and this video helped me figure out everything. 1 subscribe from me 👍👍👍

  • @sundarit7324
    @sundarit7324 2 ปีที่แล้ว

    ​ @Velcode i added set of hboxlayout with number of combobox to a vhbox layout. i have set this as layout for tablewidget. i have two seperate buttons for add and delete items. i am able to add the hboxlayout everytime when i click add. But i am unable to delete the item. please can you help me.
    Insteat of tablewidget i use frame and groupbox also... in all cases i can add the new set of hboxlayout in the existing layout. but i am not able to delete the items

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      Umm, I would have to see what you are doing. There must be something you're not deleting which keeps it there. There are various things that could be going wrong. When you delete, do you delete everything properly liked I showed in the video?

  • @fernandoi8958
    @fernandoi8958 2 ปีที่แล้ว +1

    if inside the layout you had more layouts, should you do it by recursion or just deleting the top widget is enough?

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      Good question, I am not 100% sure, but you shouldn't need to delete all the children of the widget when deleting the parent widget. This is because in Qt, when you delete the parent, the parent deletes its children (and the children of those children and so on). You can ensure this by making the upper widget the parent of the child widget. I am not sure if Qt does this automatically when you add a widget to another widget.

  • @lopo8000
    @lopo8000 2 ปีที่แล้ว

    Do u know how to when the user clicks on o radio box or on a button or label to display a video or a sound effect from a meme.

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      Never have messed with audio or video, but I think Qt has classes for those things. The click can be captured, which you can then use to show the video/sound.

    • @lopo8000
      @lopo8000 2 ปีที่แล้ว

      @@VelcodeCS is there a easy way to display a gif when a button is clicken and then hide it after a specific time?

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      ​@@lopo8000 Yes, you can use a timer that, when the timer is up, you disable the image. The image can be loaded using the Qt image loading classes (similar to icons).

  • @mohamednour-ff3it
    @mohamednour-ff3it 3 ปีที่แล้ว

    thank you, if i wanna to delete a frame what is the slot and signal should be please reply

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      What do you mean? You can add your own signal and slot which handles the removal of the frame. Is this what you're referring to?

  • @rafaelhakobyan2848
    @rafaelhakobyan2848 2 ปีที่แล้ว

    How to save current UI with different dynamics added widgets ?.Thanks !

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว +1

      I am not aware of a way to save a widget created on run time. You would have to define that yourself (store what you are trying to save on a text file and load it on the next run).

    • @rafaelhakobyan2848
      @rafaelhakobyan2848 2 ปีที่แล้ว

      @@VelcodeCS thanks for reply

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว +1

      @@rafaelhakobyan2848 no problem! Consider using a Json file for this task.

  • @lopo8000
    @lopo8000 2 ปีที่แล้ว

    i have qt 5.15.2 in Visual studio and i don't know why but i don't have any statusbar as all of the guys in the youtube tutorials how do i add one?? i*ve also puttet the question in stackoverflow

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      You're probably not using a main window and using a widget instead.

    • @lopo8000
      @lopo8000 2 ปีที่แล้ว

      @@VelcodeCS how do i add multiple images in a label?

  • @fanpeter-z3c
    @fanpeter-z3c 3 ปีที่แล้ว +5

    bro, you literally saved my ass again haha

  • @lopo8000
    @lopo8000 2 ปีที่แล้ว

    i want to redifine how do i stop example QMovie* movie("") so i can declare a new movie and shoot more movies inside like a uzi

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      I have no idea what you are talking about

  • @lopo8000
    @lopo8000 2 ปีที่แล้ว

    how do i add a radio button box and then say to add oder subbuttons to say how much gold i want

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      I have no idea what you're doing.

    • @lopo8000
      @lopo8000 2 ปีที่แล้ว

      @@VelcodeCS let's say i click on change camera hack mod and i want to set it to 2 one click sets it to 1 but i also want to set it to 3,4 and so on

  • @heartminer5487
    @heartminer5487 ปีที่แล้ว

    thx algorithm... was just thinking about this

  • @wraith188
    @wraith188 2 ปีที่แล้ว

    how to make a counter active checkBox?

  • @jeffchungwailoong
    @jeffchungwailoong 3 ปีที่แล้ว +1

    good tutorial ! save my time a lot ! keep it up 👍

  • @lopo8000
    @lopo8000 2 ปีที่แล้ว

    if i add gifs and pictures from my resource file to my qt project how do i compress the resource file so my app is smaller in MB and also works flawlessly

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      You could compress your images

  • @jotarojoestar1399
    @jotarojoestar1399 5 หลายเดือนก่อน

    can you do this on python?

  • @imranalimran8785
    @imranalimran8785 2 ปีที่แล้ว

    Hi there,
    somehow my qobject_cast() seems not to be worked and it makes the application crashed.
    why is it happenning I don't know. If I comment out the line then it works.
    void MainWindow::onAddWidget()
    {
    QVBoxLayout *layout = qobject_cast(ui->frame->layout());
    QString buttonText = tr("Button #%1").arg(layout->count());
    QPushButton* button = new QPushButton("buttonText",ui->frame);
    layout->insertWidget(0,button);
    }
    I am using qt 6.4.0 mingw 64
    Thanks in advance💕💕

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      So it's probably one of two possible issues, your ui frame's layout is not a QVBoxLayout or your ui object is null. Makes sure your widget is properly set up and you're using the right classes.

  • @chanyanhan7791
    @chanyanhan7791 3 ปีที่แล้ว

    How to do when click the button it show whole layout ( which include label inside) ?

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      In the second part of the video, you can see how to do that. Now, if you're simply trying to make it visible, then you can enable/disable it instead of creating and adding. It should be simpler.

    • @chanyanhan7791
      @chanyanhan7791 3 ปีที่แล้ว

      @@VelcodeCS Thanks a lot ! You saved my life :")

  • @Mdzab2000
    @Mdzab2000 3 ปีที่แล้ว

    Can i dynamically add QTableWidget and insert /modify/change its items(contents) from another slot using QHash map

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +1

      Yes it is possible to do so. You have to also set the headers (horizontal and vertical, whichever you need). Then you can insert items as rows.

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      Saw your comment, I am assuming you figured it out?

    • @Mdzab2000
      @Mdzab2000 3 ปีที่แล้ว

      @@VelcodeCS yes.. thank you.. made a mistake of not inserting the table onto a QHash.. so solved it.. thank you for all your input.. it really changed the way i use my logic while coding..although i have a new additional questions... Is it a good practice to have lot of QHashs for various dynamic widgets.. will it impact performance in long run.. do i need to keep checking if those Qhash maps are being used..and if not should i erase them to free up memory spaces due to those unused QHash memory pointers

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      @@Mdzab2000 I like to use hash maps to quickly have access to different items. Ideally, you want to remove items from the map as you stop using them. For me, when I use them, when I create the item (say, a widget in your case), I add it to the hash map. Then, once I no longer need it, I remove it as part of its deletion (right before it). There is no benefit to keep unused data any more. Not sure what you are implementing, but it's how I generally use the QHash. Create, insert, use, remove, delete. Keep it clean! :D

    • @sundarit7324
      @sundarit7324 2 ปีที่แล้ว

      ​@@VelcodeCS i added set of hboxlayout with number of combobox to a vhbox layout. i have two seperate buttons for add and delete items. i am able to add the hboxlayout everytime when i click add. But i am unable to delete the item. please can you help me.

  • @arda8226
    @arda8226 ปีที่แล้ว +1

    comment for support . You really help . I know that i can find in the net but its too faster.

  • @PeterHine
    @PeterHine 2 ปีที่แล้ว

    Very good. Insert item at number n would be good to know how to do

    • @PeterHine
      @PeterHine 2 ปีที่แล้ว

      It turns out insertWidget is a thing in pyside6, but no doco seems to have it

  • @zName1
    @zName1 2 ปีที่แล้ว

    What is the difference between a frame and a layout? They seem the same to me.

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว +1

      Frames have visual properties (background color, margins, etc.). Layouts do not. Frames also have a main layout within them to organize the UI elements within them (which is off by default). I hope this helps.

  • @S-Pohilenko
    @S-Pohilenko 3 ปีที่แล้ว +1

    Hello! You can publish the project somewhere?

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +1

      I'll put it on a GitHub repository and let you know.

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +1

      I put a link to the repository on the video description :)

    • @S-Pohilenko
      @S-Pohilenko 3 ปีที่แล้ว

      @@VelcodeCS Thank you very much!

    • @S-Pohilenko
      @S-Pohilenko 3 ปีที่แล้ว

      @@VelcodeCS maybe it's impudent. but I want to ask one more question. can I add buttons to the beginning of the model and not to the end? I pasted verticalSpaser into my layout so the buttons are at the top. and buttons are added from the bottom = /

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +1

      @@S-Pohilenko I'm not sure exactly what you're trying to do. However, in a container (with a layout), items are indexed based on their position (top to bottom in vertical, left to right in horizontal). So, when you add items, you simply put the index of where in the layout it should be inserted to. Do consider that spacers, although not visible, are part of the items in the layout (it has an index as well).
      So, if you want the new added buttons at the very top, you always insert at 0. If you want inserted at the bottom of all the previously inserted buttons, but above the spacer, you insert at number of items - 2.

  • @Egor-tm2zz
    @Egor-tm2zz 2 ปีที่แล้ว

    Bro can you tell me how to do this on PyQt5/Pyside 2?

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      I would, but I myself have never touched PyQt 😬

  • @arda8226
    @arda8226 ปีที่แล้ว

    is this qt 5 or 6 ?

    • @VelcodeCS
      @VelcodeCS  ปีที่แล้ว +1

      Should work for both

  • @ShilaKillAw
    @ShilaKillAw 3 ปีที่แล้ว

    Is this posible to do the same with python?

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      🤔 I myself have never used Qt with Python, but I don't imagine the API being different. Would have to give it a try.

    • @rryann088
      @rryann088 3 ปีที่แล้ว

      @@VelcodeCS Could you please show this exact thing in python? I have been struggling with it for months :' Thank you !

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +1

      @@rryann088 That would be an interesting video to make! Although I myself haven't messed around with Qt and Python.

    • @rryann088
      @rryann088 3 ปีที่แล้ว +1

      @@VelcodeCS Thank you so much for your response! ❤️ I would like to request you to make it, please :) And it is truly highly appreciated from my side - sure please take your time, i know you got this 😊 tbh I will be waiting for it 👍

  • @garipprogrammer8128
    @garipprogrammer8128 2 ปีที่แล้ว

    thank you very very much !! cute sheep btw 😄

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      Thank you 😅

  • @tonylozada5241
    @tonylozada5241 3 ปีที่แล้ว

    Great, good job guy

  • @harshitpawar7752
    @harshitpawar7752 3 ปีที่แล้ว

    QPushButton gives allocation error

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      Any way you can share your setup to see what the problem may be? I've been doing this in my projects with no issue.

    • @전승권-s6i
      @전승권-s6i 6 หลายเดือนก่อน

      you may need to add #include on your .h code. idk why but it worked :)

  • @marcodalcanto7941
    @marcodalcanto7941 3 ปีที่แล้ว

    thank you very much

  • @shameljij1224
    @shameljij1224 3 ปีที่แล้ว

    Can you show us how to make a flowchart application please. I'd pay for it. Patreon or whatever you like. thanks.

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      An application to make flowcharts?

    • @shameljij1224
      @shameljij1224 3 ปีที่แล้ว

      @@VelcodeCS just like the diagram scene example in the QT Creator example. thank you

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว

      Do you have any specification of the software you're trying to implement?

    • @shameljij1224
      @shameljij1224 3 ปีที่แล้ว

      @@VelcodeCS It's exactly like the (Diagram Scene Example) from the examples that come with Qt Creator.

  • @wraith188
    @wraith188 2 ปีที่แล้ว

    how to delete all buttons, after one click

    • @VelcodeCS
      @VelcodeCS  2 ปีที่แล้ว

      You can follow the same logic, just loop through all the things you want to delete.

  • @ivanguerrero9815
    @ivanguerrero9815 3 ปีที่แล้ว

    Can you show us how to make it in python?

    • @VelcodeCS
      @VelcodeCS  3 ปีที่แล้ว +2

      Not a bad idea! I've been wanting to work on some more Qt videos but have been busy. Fortunately the semester is over, so I should have some time to make some more!

  • @ccf19881030
    @ccf19881030 3 ปีที่แล้ว

    Thank you very much