[Qt-interest] qtreewidget

phil prentice philp.cheer at talktalk.net
Thu Jul 1 11:09:02 CEST 2010


Thanks Jason for your help.

I think that we will be OK around the fonts in that we ship the fonts with the 
system and in theory they are not meant to play around with them....but I do 
agree that what I am doing is very bad!!!!  Whats even worse is that I am 
hard coding the column widths of my tree widget to fit the text. 

I have tried many times in the past to use the font-metrics to enable me to 
calculate the width, but I never seem to get things to work.  Take my current 
problem.  My first column has the title "Width" so currently I hard code the 
width to be 50 pixels using

m_netRoutingTreeWidget->setColumnWidth(0, 50);

If I try and use fontmetrics i.e.

QTreeWidgetItem *header = m_netRoutingTreeWidget->headerItem();
QFont font = header->font(0);
QFontMetrics fp(font);
m_netRoutingTreeWidget->setColumnWidth(0, fp.width("Active"));

The column comes out no where near big enough.  In fact the pixel value it is 
trying to use is 33  no where near to the 50 I really need.

Am I doing this the right way?
Note I must admit that I am using a fairly old version of Qt i.e.   Version 
4.2,  but I think its more likely that I am doing something else wrong.

Thanks again
Phil

On Wednesday 30 June 2010 17:23, you wrote:
> Ah, yes. bad ideas all the way around, but only for one reason:
> The user decides the font and size used.
>
> You have to calculate the number of spaces in your hack to match. The
> biggest differnce is fixed vs non-fixed-width fonts. You should be able to
> get te widget pos() of the "active" "type" and "module" widgets and use
> QFontMetrics to propery space out "Network" "Chan Wiring" and "Chan
> Routing" in a string. Though As a hack, I's probablt se a table, and align
> one left  in a cell, one center in a cell and one right in a cell and be
> done with it.
>
>
>
>
>
> ----- Original Message ----
> From: phil prentice <philp.cheer at talktalk.net>
> To: qt-interest at trolltech.com
> Sent: Wed, June 30, 2010 12:27:13 PM
> Subject: [Qt-interest] qtreewidget
>
> Hi
>   I am producing a dialog which uses a QTreeWidget.  I have a number of
> questions regarding it. I have included the main function that is working
> with the QTreeWidget.
> The main problem that I have is that my table has two sets of headings.
> A top level heading that sits on a group of lower level headings...
>
> First and second rows are headings...data follows.
>
> |      NETWORK        |       CHAN WIRING   |   CHAN ROUTING |
> | Active |    Name      |  Type  | Number        |  Module | Highway |
> | True    |   FRED1     |  DIG   |  100             |    DS      |    DF   
> |  | True    |   FRED1     |  DIG   |  100             |    DS      |    DF
> |     |
>
> .....
>
> Using QTreeWidget is there any clever way of handling these headings?  I
> could not see anything that would help me.
>
> What I did in the end was fix each of the column widths in pixels and
> created the one QLABEL =  "NETWORK           CHAN WIRING     CHAN ROUTING"
> and suitably spaced it out to appear above the QTreeWidget columns. I
> stopped the dialogs width from being modified and fixed it at an
> appropriate width. It seems to work, but I was wondering if there is a
> better way?
>
> I know that I am depending on the font being the same, but in my case this
> should not be a problem.
>
> Is it bad programming practise that I have fixed the column widths or is
> that perfectly acceptable?
>
> Any help appreciated
>
> Phil
>
> Code:-
> void NetIdDialog::createAndLayoutNetworkRoutingTab()
> {
>   // Create "NETWORK INFO & ROUTING" widgets.
>   m_networkLabel = new QLabel(
>         "                    NETWORK                                  "
>                                 "CHANNEL WIRING                  "
>                                 "CHANNEL ROUTING");
>   m_netRoutingTreeWidget = new QTreeWidget();
>   m_netRouting = new QWidget();
>
>   // Add headings
>   m_netRoutingTreeWidget->setColumnCount(7);
>   QTreeWidgetItem *header = m_netRoutingTreeWidget->headerItem();
>   header->setText(0, "Active");
>   header->setText(1, "Name");
>   header->setText(2, "Type");
>   header->setText(3, "Number");
>   header->setText(4, "Force/Sense");
>   header->setText(5, "Module");
>   header->setText(6, "Routing Highway");
>   setMaximumWidth(725);
>   setMinimumWidth(725);
>
>   m_netRoutingTreeWidget->setColumnWidth(0, 50);
>   m_netRoutingTreeWidget->setColumnWidth(1, 190);
>   m_netRoutingTreeWidget->setColumnWidth(2, 50);
>   m_netRoutingTreeWidget->setColumnWidth(3, 70);
>   m_netRoutingTreeWidget->setColumnWidth(4, 100);
>   m_netRoutingTreeWidget->setColumnWidth(5, 70);
>   m_netRoutingTreeWidget->setColumnWidth(6, 200);
>
>   // Do Layout.
>   QHBoxLayout *firstRowLayout = new QHBoxLayout;
>   firstRowLayout->addWidget(m_networkLabel);
>   firstRowLayout->addStretch();
>
>   QVBoxLayout *mainLayout = new QVBoxLayout;
>   mainLayout->addLayout(firstRowLayout);
>   mainLayout->addWidget(m_netRoutingTreeWidget);
>   m_netRouting->setLayout(mainLayout);
> }
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list