Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, October 2006
dummy newbie cannot make a single pushbutton show up!


Message 1 in thread

Why doesn't the following code produce a window with a push button in  
it?
Thanks


int main(int argc, char *argv[]) {
   QApplication app(argc, argv);
   QMainWindow blah;
   QHBoxLayout *layout = new QHBoxLayout;
   layout->addWidget(new QPushButton("blah"));
   blah.setLayout(layout);
   blah.show();
   return app.exec();
}

-- 
 [ signature omitted ] 

Message 2 in thread

Rich,

No worries, common newbie mistake. QMainWindow is a convenience widget 
made up of QWidget + a prearragned QGridLayout for menu bar, status 
bar,  toolbars, dock type areas + lazy creation of said menu bar and 
status bar.  It is not the general toplevel window widget.  QWidget will 
work just fine for that.  You have two options with your code:

1) Use QMainWindow's layouts
int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  QMainWindow blah;
  //No need for a layout QMainWindow already has one
  // But we do need to tell QMainWindow that we want our widget in the 
center of the grid.
  blah->setCentralWidget(new QPushButton("blah"));
  blah.show();
  return app.exec();
}

QMainWindow can only have one "Central Widget", but this is not a 
limitation since it can be a QWidget with a layout and N children.

2) Use QWidget with your own layouts
int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  QWidget blah(0);
  QHBoxLayout *layout = new QHBoxLayout;
  layout->addWidget(new QPushButton("blah"));
  blah.setLayout(layout);
  blah.show();
  return app.exec();
}

Good Luck,
--Justin


Rich Cook wrote:
> Why doesn't the following code produce a window with a push button in 
> it? 
> Thanks
>
>
> int main(int argc, char *argv[]) {
>   QApplication app(argc, argv);
>   QMainWindow blah;
>   QHBoxLayout *layout = new QHBoxLayout;
>   layout->addWidget(new QPushButton("blah"));
>   blah.setLayout(layout);
>   blah.show();
>   return app.exec();
> }
>
> -- 
> "There's no time to stop for gas, we're already late"-- Karin Donker
> --
> Rich "wealthychef" Cook
> <http://web.mac.com/wealthychef/iWeb/Site/Urinetown.html>
> 925-784-3077
> --
>
>
>

begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:(ICS) Integrated Computer Solutions ;Engineering
adr:;;54B Middlesex Turnpike;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Senior Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
x-mozilla-html:FALSE
url:http://www.ics.com
version:2.1
end:vcard


Message 3 in thread

Thanks,
Can you tell me why the following does not show a push button?

class MainWindow: public  QMainWindow {
   Q_OBJECT
public:
   MainWindow(QWidget *parent=NULL): QMainWindow(parent) {

     setCentralWidget(new QWidget(this));
     QHBoxLayout *layout = new QHBoxLayout;
     changer = new QPushButton("blah");
     layout->addWidget(changer);
     centralWidget()->setLayout(layout);
     return;
   }
private slots:
void on_changer_clicked(){
   return;
}

public:	
   QPushButton *changer;
};

int main(int argc, char *argv[]) {
   QApplication app(argc, argv);
   QMainWindow blah;
   blah.show();
   return app.exec();
}

On Oct 2, 2006, at 6:24 PM, Justin Noel wrote:

> Rich,
>
> No worries, common newbie mistake. QMainWindow is a convenience  
> widget made up of QWidget + a prearragned QGridLayout for menu bar,  
> status bar,  toolbars, dock type areas + lazy creation of said menu  
> bar and status bar.  It is not the general toplevel window widget.   
> QWidget will work just fine for that.  You have two options with  
> your code:
>
> 1) Use QMainWindow's layouts
> int main(int argc, char *argv[]) {
>  QApplication app(argc, argv);
>  QMainWindow blah;
>  //No need for a layout QMainWindow already has one
>  // But we do need to tell QMainWindow that we want our widget in  
> the center of the grid.
>  blah->setCentralWidget(new QPushButton("blah"));
>  blah.show();
>  return app.exec();
> }
>
> QMainWindow can only have one "Central Widget", but this is not a  
> limitation since it can be a QWidget with a layout and N children.
>
> 2) Use QWidget with your own layouts
> int main(int argc, char *argv[]) {
>  QApplication app(argc, argv);
>  QWidget blah(0);
>  QHBoxLayout *layout = new QHBoxLayout;
>  layout->addWidget(new QPushButton("blah"));
>  blah.setLayout(layout);
>  blah.show();
>  return app.exec();
> }
>
> Good Luck,
> --Justin
>
>
> Rich Cook wrote:
>> Why doesn't the following code produce a window with a push button  
>> in it? Thanks
>>
>>
>> int main(int argc, char *argv[]) {
>>   QApplication app(argc, argv);
>>   QMainWindow blah;
>>   QHBoxLayout *layout = new QHBoxLayout;
>>   layout->addWidget(new QPushButton("blah"));
>>   blah.setLayout(layout);
>>   blah.show();
>>   return app.exec();
>> }
>>
>> -- 
>> "There's no time to stop for gas, we're already late"-- Karin Donker
>> --
>> Rich "wealthychef" Cook
>> <http://web.mac.com/wealthychef/iWeb/Site/Urinetown.html>
>> 925-784-3077
>> --
>>
>>
>>
>
> <justin.vcf>

-- 
 [ signature omitted ] 

Message 4 in thread

Rich,

Rich Cook wrote:
> Can you tell me why the following does not show a push button?  
>
> class MainWindow: public  QMainWindow {
>  
> ...
> }
>  
> int main(int argc, char *argv[]) {
>   QApplication app(argc, argv);
>   QMainWindow blah;
>   blah.show();
>   return app.exec();
> }
>

Typo on QMainWindow in main.  That ought to be MainWindow ;)

--Justin
begin:vcard
begin:vcard
fn:Justin Noel
n:Noel;Justin
org:(ICS) Integrated Computer Solutions ;Engineering
adr:;;54B Middlesex Turnpike;Bedford;MA;01730;USA
email;internet:justin@xxxxxxx
title:Senior Consulting Engineer / Certified Qt Instructor
tel;work:(617) 621-0060
x-mozilla-html:FALSE
url:http://www.ics.com
version:2.1
end:vcard