Error in connecting Signals and slots qt6 lineEdit
-
@yegender Because it's wrong.
If you really want to use the old connect syntax then do it properly:QObject::connect(Search, SIGNAL(textChanged(const QString)), this,SLOT(ChangeSearchStyleSheet(const QString)));
Better to use "new" Qt5 connect syntax.
-
@yegender said in Error in connecting Signals and slots qt6 lineEdit:
QObject::connect(Search, SIGNAL(&QLineEdit::textChanged),
this,SLOT(&YoutubeTopBar::ChangeSearchStyleSheet));Just use the "new" syntax:
QObject::connect(Search, &QLineEdit::textChanged,
this, &YoutubeTopBar::ChangeSearchStyleSheet);Assuming that "Search" is a pointer-to-QLineEdit
-
@yegender said in Error in connecting Signals and slots qt6 lineEdit:
None of new and old are working
Show the code.
And please show us also YoutubeTopBar definition else nobody know what you have there... -
Here is the code:
Header:
#ifndef YOUTUBETOPBAR_H #define YOUTUBETOPBAR_H #include <QWidget> #include <QVBoxLayout> #include <QHBoxLayout> #include <QLabel> #include <QLineEdit> #include <QToolButton> #include <QObject> class YoutubeTopBar : public QWidget { Q_OBJECT public: explicit YoutubeTopBar(QWidget *parent = nullptr); QHBoxLayout mainLayout; QToolButton ToogleLeftPane; QToolButton YoutubeHome; QToolButton ToogleDownloadManager; QToolButton ToogleSettings; QLineEdit Search; public slots: void ChangeSearchStyleSheet(); signals: }; #endif // YOUTUBELEFTPANE_H
Source File:
#include "youtubetopbar.h" YoutubeTopBar::YoutubeTopBar(QWidget *parent) : QWidget{parent} { this->setWindowTitle("YT Top Bar"); this->setLayout(&mainLayout); mainLayout.setMargin(0); mainLayout.setSpacing(0); mainLayout.addWidget(&ToogleLeftPane); mainLayout.addWidget(&YoutubeHome); mainLayout.addWidget(&Search); mainLayout.addWidget(&ToogleDownloadManager); mainLayout.addWidget(&ToogleSettings); ToogleLeftPane.setText("Toogle Left Pane"); ToogleLeftPane.setStyleSheet("QToolButton{" "background-color:rgba(0,0,0,0.6);" "color:white;" "border:none;" "margin:5px;" "padding:10px;" "}" "QToolButton::hover{" "background-color:rgba(255,255,255,0.1);" "border-radius:5px;" "}"); YoutubeHome.setText("Youtube"); YoutubeHome.setStyleSheet("QToolButton{" "background-color:rgba(0,0,0,0.6);" "color:white;" "border:none;" "margin:5px;" "padding:10px;" "}" "QToolButton::hover{" "background-color:rgba(255,255,255,0.1);" "border-radius:5px;" "}"); ToogleSettings.setText("Settings"); ToogleSettings.setStyleSheet("QToolButton{" "background-color:rgba(0,0,0,0.6);" "color:white;" "border:none;" "margin:5px;" "padding:10px;" "}" "QToolButton::hover{" "background-color:rgba(255,255,255,0.1);" "border-radius:5px;" "}"); ToogleDownloadManager.setText("Download Manager"); ToogleDownloadManager.setStyleSheet("QToolButton{" "background-color:rgba(0,0,0,0.6);" "color:white;" "border:none;" "margin:5px;" "padding:10px;" "}" "QToolButton::hover{" "background-color:rgba(255,255,255,0.1);" "border-radius:5px;" "}"); Search.setPlaceholderText("Search"); Search.setStyleSheet("QLineEdit{" "background-color:rgba(0,0,0,0.3);" "border:none;" "margin:5px;" "padding:10px;" "border-radius:5px;" "}" "QLineEdit::hover{" "background-color:rgba(255,255,255,0.1);" "border-radius:5px;" "}"); QObject::connect(Search, &QLineEdit::textChanged),this,&YoutubeTopBar::ChangeSearchStyleSheet); } void YoutubeTopBar::ChangeSearchStyleSheet(){ if(Search.text().isEmpty()){ Search.setStyleSheet("color:rgba(255,255,255,0.6);"); } else { Search.setStyleSheet("color:white;"); } }
CMAKE:
cmake_minimum_required(VERSION 3.5) project(video-player VERSION 0.1 LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h youtubeleftpane.h youtubeleftpane.cpp youtubetopbar.h youtubetopbar.cpp youtubebanner.h youtubebanner.cpp youtubepage.h youtubepage.cpp youtubetopbarwrapper.h youtubetopbarwrapper.cpp ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(video-player MANUAL_FINALIZATION ${PROJECT_SOURCES} ) # Define target properties for Android with Qt 6 as: # set_property(TARGET video-player APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR # ${CMAKE_CURRENT_SOURCE_DIR}/android) # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation else() if(ANDROID) add_library(video-player SHARED ${PROJECT_SOURCES} ) # Define properties for Android with Qt 5 after find_package() calls as: # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") else() add_executable(video-player ${PROJECT_SOURCES} ) endif() endif() target_link_libraries(video-player PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) set_target_properties(video-player PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(video-player) endif()
And I am using this class widget inside a layout of another widget
Thank you
-
@yegender said in Error in connecting Signals and slots qt6 lineEdit:
QObject::connect(Search, &QLineEdit::textChanged),this,&YoutubeTopBar::ChangeSearchStyleSheet);
Please just count your parentheses or copy/paste what was suggested to you, nobody wrote the above. Hence the error message.
-
@yegender said in Error in connecting Signals and slots qt6 lineEdit:
QObject::connect(Search, &QLineEdit::textChanged),this,&YoutubeTopBar::ChangeSearchStyleSheet);
This does not even compile...
Also please create objects on the heap instead adding them as objects in your class - it will most likely crash when the YoutubeTopBar object gets destroyed since then the children are beeing deleted automatically which will not work in your case - see QObject Trees and Ownership.
-
@yegender said in Error in connecting Signals and slots qt6 lineEdit:
@JonB Same error
What error? Fix your code and post your real code + compiler error.
-
Because
Search
is an object, not a pointer - pass a pointer because QObject::connect() wants pointers.