Tutorials/C Programming Tutorial/How to use a QLayOut

From ThorstensHome
Revision as of 09:21, 7 April 2008 by WikiSysop (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a very minimalistic QLayOut coding demonstration - you can use it to learn, and as a template. http://doc.trolltech.com/4.3/qlayout.html shows you a lot, but wordings like "Adds layout to the end of the box, with serial stretch factor stretch" do not help to get things actually going. So, you need a little example - here it is.

main.cpp

#include <QBoxLayout>
#include <QTreeWidget>
#include <kapplication.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <KMainWindow>

int main (int argc, char *argv[])
{
  const QByteArray& ba=QByteArray("test");
  const KLocalizedString name=ki18n("myName");
  KAboutData aboutData( ba, ba, name, ba, name);
  KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication khello;
  KMainWindow* mainwindow=new KMainWindow();
  QWidget* mywidget=new QWidget();
  QTreeWidget* qw=new QTreeWidget(mywidget);
  qw->setColumnCount(3);
  QStringList columns;
  columns << "first column" << "second column" << "third column";
  QTreeWidgetItem* item=new QTreeWidgetItem(qw,columns);
  QTreeWidgetItem* item2=new QTreeWidgetItem(item,columns);
  qw->addTopLevelItem(item);
  QGridLayout* layOut=new QGridLayout(mywidget);
  layOut->addWidget(qw);
  mainwindow->setCentralWidget(mywidget);
  mainwindow->show();
 //item->takeChild(0);
  return khello.exec();
}

CMakeLists.txt

PROJECT( tutorial )
FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )


SET(tutorialSources main.cpp )

KDE4_ADD_EXECUTABLE(tutorial ${tutorialSources} )

TARGET_LINK_LIBRARIES(tutorial ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )

Compile, link and run this

cmake . && make && ./tutorial