Solved Error: The program has unexpectedly finished.
-
Why after initializing "home" pointer by calling "setupWidgets(home,homepanelShadow,homepanel,50,":/Images/Home.png");
"
it crashes on this statement :- "home->setStyleSheet("background-color:red;"); // this is where it crashes"
below is the code :-ramadantimes.cpp
RamadanTimes::RamadanTimes(QWidget *parent) : QMainWindow(parent), ui(new Ui::RamadanTimes) { ui->setupUi(this); setupWidgets(home,homepanelShadow,homepanel,50,":/Images/Home.png"); home->setStyleSheet("background-color:red;"); // this is where it crashes } void RamadanTimes::setupWidgets(QToolButton *button,QGraphicsDropShadowEffect *panelshadow, QFrame *panel,int w,QString image){ button = constructButton(button,w,image); panelshadow = createShadow(panelshadow,0,0,5); panel = constructFrame(panel,panelshadow); if (w != 50) panel->hide(); } QToolButton *RamadanTimes::constructButton(QToolButton *button,int y,QString image){ button = new QToolButton(menubar); button->setGeometry(0,y,60,50); button->setIcon(QIcon(image)); button->setIconSize(QSize(35,35)); button->setToolButtonStyle(Qt::ToolButtonIconOnly); button->setText("Home"); button->setAutoRaise(true); button->setAutoRepeat(false); button->setStyleSheet("QToolButton:pressed { background-color: rgb(42,52,62); border:0px;}"); return button ; } QGraphicsDropShadowEffect *RamadanTimes::createShadow(QGraphicsDropShadowEffect *shadow,int x, int y, int radius, QString color){ shadow = new QGraphicsDropShadowEffect ; shadow->setXOffset(x); shadow->setYOffset(y); shadow->setBlurRadius(radius); shadow->setColor(color); return shadow ; } QFrame *RamadanTimes::constructFrame(QFrame *frame,QGraphicsDropShadowEffect *shadow){ frame = new QFrame(this); frame->setGeometry(75,70,515,440); frame->setGraphicsEffect(shadow); return frame ; }
-
@Ahti It's probably a problem with your memory management. It looks to me like you have a pointer to a button as a member in RamadanTimes. Follow the values of that pointer variable and each *button variable with qDebug() << home and qDebug() << button statements in each function and you may find a problem.
-
@Eeli-K qDebug() << home ; displays "QWidget(0x0)" instead of an address of "home" pointer
-
You pass the pointer by value, the
button =
assignment will not be reflected outsidesetupWidgets
change
RamadanTimes::setupWidgets(QToolButton *button,QGraphicsDropShadowEffect *panelshadow,QFrame *panel,int w,QString image)
toRamadanTimes::setupWidgets(QToolButton*& button,QGraphicsDropShadowEffect*& panelshadow,QFrame*& panel,int w,const QString& image)
-
@Ahti VRonin's solution is good if you want to do minimal changes. On the other hand you have strange architecture - why you pass the members as arguments at all to member functions? You could do
void RamadanTimes::setupWidgets(int w,QString image){ home = new QToolButton(menubar); home->setGeometry... // init the button... panelshadow = new... // init the shadow... panel = new... //init the frame
-
@Eeli-K Because i have more QWidgets not just one.
-
@VRonin Thanks brother :D love you :)
-
@Ahti I don't understand. What QWidgets you mean? If you have several RamadanTimes objects (which are QMainWindows) they each have their own buttons etc. The main point of objects (and classes) is to encapsulate data and behaviour into one object. The member data of each object can be accessed in member functions so that it doesn't have anything to do with other objects of the same class. Static members are class-wide, shared between objects of the same class. One just shouldn't pass normal data members as arguments to normal member functions because they can be accessed directly by the member functions.
-
this is what i mean brother:
setupWidgets(home,homepanelShadow,homepanel,50,":/Images/Home.png"); setupWidgets(settings,settingspanelShadow,settingspanel,150,":/Images/settings.png"); setupWidgets(contact,contactpanelShadow,contactpanel,250,":/Images/Contact.png"); setupWidgets(about,aboutpanelShadow,aboutpanel,350,":/Images/About.png");
-
@Ahti OK, now I understand better. The case is closed.