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. Signals won't trigger see code below (I am using Qt on OS X 10.8 amples app are working fine but this one won't...)
Qt 6.11 is out! See what's new in the release blog

Signals won't trigger see code below (I am using Qt on OS X 10.8 amples app are working fine but this one won't...)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k 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
    lvignals
    wrote on last edited by
    #1

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    //#include <QDialog>
    #include <QtGui>

    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;

    class MainWindow : public QDialog
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    void textChanged(const QString &str);

    private slots:
    void findClicked();
    void enableFindButton(const QString &text);

    private:
    QLabel *debugLabel;
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
    };

    #endif // MAINWINDOW_H

    #include <QtGui>
    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
    {
    debugLabel = new QLabel(tr("no debug info yet..."));
    label = new QLabel(tr("Find &what"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));
    
    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    
    closeButton = new QPushButton(tr("Close"));
    
    
    /*bool r=false;
    r = QObject::connect(pObj1,SIGNAL(signalStuff()),pObj2,SLOT(gotStuff()));
    Q_ASSERT(r);*/
    
    bool r=false;
    r = connect(lineEdit, SIGNAL(textChanged(QString &text)), this, SLOT(enableFindButton(QString &text)));
    Q_ASSERT(r);
    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
    
    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);
    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);
    leftLayout->addWidget(debugLabel);
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
    
    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
    

    }

    MainWindow::~MainWindow()
    {

    }

    void MainWindow::findClicked()
    {
    debugLabel->setText(tr("findClicked()"));
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
    if (backwardCheckBox->isChecked()) {
    emit findPrevious(text, cs);
    } else {
    emit findNext(text, cs);
    }
    }

    void MainWindow::enableFindButton(const QString &text) {
    debugLabel->setText(tr("enableFindButton()"));
    findButton->setEnabled(!text.isEmpty());
    }

    @

    Thanks for your help,
    Ludovic Vignals

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lvignals
      wrote on last edited by
      #2

      Ok found my two problems:

      1. only custom signals need to be declared in class header file so I removed th below signal declaration
        @
        //void textChanged(const QString &str);
        @

      2. in the connect() that takes signal and slot with parameters I did not include the const keywork in front of the arguments the changed line look like this:
        @
        connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));
        @

      After making those changes the connection worked as expected.
      Ludovic Vignals

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        You need to match the call pattern exactly as it is.
        The documentation of the "textChanged signal":http://qt-project.org/doc/qt-4.8/qlineedit.html#textChanged sais (const QString &), but you used (QString &). You should also leave the variable's name away.

        rewrite to
        @
        bool r=false;
        r = connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
        Q_ASSERT(r);
        @
        The "signal clicked":http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked has a bool in the call list. Use "pressed" or "released" signal. Their parameter list is empty.

        [too late]

        Vote the answer(s) that helped you to solve your issue(s)

        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