Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Polish
  4. [SOLVED] QLabel i niedziałające label->setText...

[SOLVED] QLabel i niedziałające label->setText...

Scheduled Pinned Locked Moved Polish
6 Posts 2 Posters 3.9k Views 1 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
    Lukasyno
    wrote on last edited by
    #1

    Witam, natrafiłem na problem podobny do tego opisanego w pierwszym poście tego wątku:

    http://qt-project.org/forums/viewthread/15774

    @#include <QtGui>

    int main( int argc, char** args )
    {
    QApplication app( argc, args );

    QLabel *label = new QLabel;
    label->setText( "Ready." );

    QPushButton *button = new QPushButton;
    button->setText( "Change text" );
    QObject::connect( button, SIGNAL( clicked() ), label, SLOT( setText( "Boo!" ) ) );

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget( button );
    layout->addWidget( label );

    QMainWindow *mainWindow = new QMainWindow;
    QWidget *centralWidget = new QWidget;
    centralWidget->setLayout( layout );
    mainWindow->setCentralWidget( centralWidget );
    mainWindow->show();

    return app.exec();
    }
    @

    jak sprawić by to zadziałało? próbowałem pisząc osobną funkcję (sygnały i sloty - tyle samo parametrów)

    @void button_clicked() { label->setText("Boo!") }
    @

    ale to nie działa. gdzie leży problem?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Z wyjątkiem nowej składni i funkcji Lambda w połączeniach, nie wolno przekazywać żadnych parametrów do Slotu przy deklaracji połączenia. Kod wyjkonawczy musi znajdować się w samej funkcji slotu, lub sygnał musi byc wysłany z odpowiednimi danymi.

      Twoje podejście z button_clicked() jest dobre, jedyny błąd jest taki, że MOC nigdy nie skanuje pliku main.cpp na obecność meta-kodu. Musisz przenieść tą metodę do oddzielnego pliku nagłówkowego, najlepiej do własnej klasy dziedziczącej po QObject, inaczej Meta Object System po prostu nie wie, że istnieje taki Slot.

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lukasyno
        wrote on last edited by
        #3

        Hmm.. coś takiego?

        @
        class ObjectLabel;
        class myDialog: public QDialog
        {
        Q_OBJECT
        friend class ObjectLabel;
        public:
        myDialog();
        private:
        QPushButton *button;

        };

        class ObjectLabel : public QObject
        {

        Q_OBJECT
        
        friend class Dialog;
        

        public:
        ObjectLabel(const QString&);
        QLabel* poka_label() { return label1; }
        private:
        QLabel *label1;
        private slots:
        void button_clicked()
        {
        if(poka_label()->text() !="Boo")
        {
        label1->setText("Boo");
        }
        }
        };
        @

        potem w konstruktorze okna:
        @
        QVBoxLayout *mainL = new QVBoxLayout(this);
        button = new QPushButton("ChangeText");

        ObjectLabel *lab = new ObjectLabel("nic");
        
        mainL->addWidget(button);
        
        mainL->addWidget(lab);
        this->setLayout(mainL);
        

        connect(OK, SIGNAL(clicked()), label, SLOT(ObjectLabel::button_clicked() ) );
        @

        próbowałem w ten sposób, jednak nie działa metoda mainL->addWidget(lab) , gdy próbuję
        dodać do layoutu obiekt klasy ObjectLabel.
        próbowałem inaczej - dziedziczyć po QWidget, bo wtedy mainL->addWidget(lab) jakoś działa, jednak program tego labela nie wyświetla. czy w takim razie trzeba dziedziczyć po dwóch klasach?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Niepotrzebnie aż tak to komplikować.
          @
          // nagłówek
          class ObjectLabel : public QLabel
          {
          Q_OBJECT

          public slots:
          void buttonClicked();
          };

          // źródełko:
          ObjectLabel::buttonClicked() {
          if (text() != "Boo")
          setText("Boo");
          }

          // main.cpp
          int main( int argc, char** args )
          {
          QApplication app( argc, args );

          ObjectLabel *label = new ObjectLabel;
          label->setText( "Ready." );

          QPushButton *button = new QPushButton;
          button->setText( "Change text" );
          QObject::connect(button, SIGNAL( clicked() ), label, SLOT( buttonClicked()));

          // ...
          @

          (Z(:^

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lukasyno
            wrote on last edited by
            #5

            Dzięki za podpowiedź, na początku label był w osobnym oknie, dodałem funkcję QLabel * all_acess { return labela; } , by jako widżet dodać bezpośrednio label, a nie ten obiekt,
            i wszystko działa.

            SIerdzio, jesteś gość :D

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Hehe, dzięki :) Miło, że się udało. Powodzenia dalej!

              (Z(:^

              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