Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. interface
Forum Updated to NodeBB v4.3 + New Features

interface

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 769 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lorence
    wrote on last edited by
    #1

    can i pass QLabel or QLineEdit to QWidget parameter?

    i have this:
    class Interface
    {
    protected:
    void forStudents(QWidget w)
    {
    w->setVisible(false);
    }
    void forStaffs(QWidget);
    }

    class A : Interface
    {
    QLabel label;
    void func();
    }
    void A::func()
    {
    forStudents(label); // doesnt work
    }
    class B : Interface
    {
    QLineEdit lineedit;
    }

    anyone can help me?

    1 Reply Last reply
    0
    • HamedH Offline
      HamedH Offline
      Hamed
      wrote on last edited by
      #2

      Hi, You can't just pass a QLable as QWidget.
      you can cast it then pass it ass parameter but it's not recommended but in some cases it helps if you use it carefully.
      it's better practice to pass exact type to function. but if you had to cast it's better to use less powerful casts of C++11 or Qt.

      HamedBabaeyan@yahoo.com
      for more interesting stuff

      1 Reply Last reply
      0
      • TheBadgerT Offline
        TheBadgerT Offline
        TheBadger
        wrote on last edited by TheBadger
        #3

        Something does not look right in the code that you supplied, perhaps a typo when creating the post or the code is incorrect, for the function I would expect:

        void forStudents(QWidget* w)  // Note the pointer to a widget
        {
            w->setVisible(false); // This will not compile without the pointer argument since you use the arrow operator ->
        }
        

        When this is done, you should be able to call the function as follow:

        void A::func()
        {
            forStudents(&label); // Taking the address of the label
        }
        

        It should work when working with pointers since QLabel is derived from QWidget.
        Hope this helps

        PS: Please use markup when posting code, it makes it easier to read and the intent clear.


        Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved